2012-05-31 18 views
2

我需要一些帮助,需要指出正确的方向。我正在创建一个应该显示二维数据的WPF应用程序。应当示出这样的:将表格中的2D数据可视化

------------------------- 
|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

试试这个:

<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"}; 
} 
+0

这工作,但不给我我需要的任何功能,如选择多个单元格,改变某些行,列,单元格等的背景。 – metacircle

+0

它应该是你的起点。 –