2

我需要一些帮助,需要指出正确的方向。我正在创建一个应该显示二维数据的 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”。我不希望有任何可能对数据进行排序(例如单击标题)。我希望能够一次选择多行或多列以将数据复制到剪贴板。数据不得编辑。还有一些样式会很好,比如第一行和第一列必须有一些不同的背景颜色。

关于如何做到这一点的任何建议?

4

1 回答 1

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"};
}
于 2012-05-31T13:55:14.673 回答