我需要一些帮助,需要指出正确的方向。我正在创建一个应该显示二维数据的WPF应用程序。它应该像这样显示:
-------------------------
|y/x| 1 | 2 | 3 | 4 | 5 |
| 1| 1 | 2 | 3 | 4 | 5 |
| 2| 2 | 4 | 6 | 8 | 10|
| 3| 3 | 6 | 9 | 12| 15|
| 4| 4 | 8 | 12| 16| 20|
| 5| 5 | 10| 15| 20| 25|
-------------------------
一些示例代码:XAML:
<Window x:Class="WpfApplication6.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<!--Place control for 2d data here-->
</Grid>
</Window>
C#:
public partial class MainWindow : Window
{
string[,] values = new string[5,5];
string[] x = new string[5];
string[] y = new string[5];
public MainWindow()
{
InitializeComponent();
CreateArrays();
}
private void CreateArrays()
{
for (int i = 0; i < values.GetLength(0); i++)
{
for (int j = 0; j < values.GetLength(1); j++)
{
values[i, j] = ((i+1) * (j+1)).ToString();
}
}
for (int i = 0; i < x.GetLength(0); i++)
{
x[i] = (i+1).ToString();
}
for (int j = 0; j < y.GetLength(0); j++)
{
y[j] = (j+1).ToString();
}
}
}
所以第一行必须是x值,第一列是y值,其余是值。请注意,左上角的单元格包含“y / x”。我不希望有任何可能对数据进行排序(比如点击标题)。我希望能够一次选择多行和/或列来将数据复制到剪贴板。数据不得编辑。另外一些造型会很漂亮,第一行和第一列必须有一些不同的背景颜色。
有关如何执行此操作的任何建议吗?
答案 0 :(得分:0)
试试这个:
<Grid>
<ItemsControl x:Name="itemscontrol">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="6" Rows="6" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding}" Margin="5" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
并且有多种方法可以将数据输入其中 - 例如
public MainWindow()
{
InitializeComponent();
this.itemscontrol.ItemsSource = new[] {"x/y", "1","2","3","4","5",
"1","1","2","3","4","5",
"2","2","4","6","8","10",
"3","3","6","9","12","15",
"4","4","8","12","16","20",
"5","5","10","15","20","25"};
}