WPF中的表格 [英] Grid Table in WPF

查看:633
本文介绍了WPF中的表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个网格。它应该是可编辑的


并且我应该设置行数和列数。


I need to create a grid. It should be editable
And i should set row and column count.
for example

mygrid.RowCount = 3;
mygrid.ColumnCount = 3;

这里是这样的:

如何绑定二维数组到DataGrid?

How to BIND 2D array to DataGrid?

推荐答案

您可以使用WPF DataGrid控件。它显示与包含属性(列)的对象(行)集合相对应的单元格网格。您需要提供数据存储-对象的集合。集合中的对象数(集合数)将确定网格中的行数。 DataGrid支持在UI中编辑数据。

You can use the WPF DataGrid control. It displays a grid of cells that correspond to a collection of objects (rows) containing properties (columns). You need to supply the data storage - a collection of objects. The number of objects in the collection (the collection count) will determine the number of rows in the grid. The DataGrid supports editing the data in the UI.

此示例定义了三列并将它们绑定到数据对象的A,B和C属性。

This example defines three columns and binds them to the A, B, and C properties of the data object.

<DataGrid AutoGenerateColumns="False" 
          Height="200" 
          HorizontalAlignment="Left" 
          Name="dataGrid1" 
          VerticalAlignment="Top" 
          Width="200">
    <DataGrid.Columns >
            <DataGridTextColumn Binding="{Binding Path=A}" MinWidth="50" />
            <DataGridTextColumn Binding="{Binding Path=B}" MinWidth="50" />
            <DataGridTextColumn Binding="{Binding Path=C}" MinWidth="50" />
    </DataGrid.Columns>
</DataGrid>

您将需要(通过代码或使用数据绑定)将具有这些属性的对象集合分配给DataGrid的ItemsSource属性,以及其他任何ItemsControl。像这样的东西:

You will need to assign (in code or using data binding) a collection of objects with these properties to the DataGrid's ItemsSource property, as with any other ItemsControl. Something like this:

public partial class MainWindow: Window
{
        public class DataObject
        {
            public int A { get; set; }
            public int B { get; set; }
            public int C { get; set; }
        }

        public MainWindow()
        {
            InitializeComponent();

            var list = new ObservableCollection<DataObject>();
            list.Add(new DataObject() { A = 6, B = 7, C = 5 });
            list.Add(new DataObject() { A = 5, B = 8, C = 4 });
            list.Add(new DataObject() { A = 4, B = 3, C = 0 });
            this.dataGrid1.ItemsSource = list;
}

在编辑中心单元格时结果如下:

And the result looks like this, when editing the center cell:

侧面说明:WPF Grid类仅用于布局。它不提供数据编辑支持。

Side note: the WPF Grid class is only for layout. It does not provide data editing support.

这篇关于WPF中的表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆