创建自定义 DataGrid 的 ItemsSource [英] Create a custom DataGrid's ItemsSource

查看:47
本文介绍了创建自定义 DataGrid 的 ItemsSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 DataGrids,但我正在努力绑定我的数据,因为列数因必须显示的信息而异.

I am working with DataGrids but I am struggling to binding my data since the number of columns varies depending of the info that has to be showed.

所以,我试图做的是创建一个包含我在某个时候需要的所有列和行的对象,并将这个对象绑定到 ItemsSource 属性.因为我在 WindowsForms 中使用过 DataGridViews,所以我想到了这样的事情:

So, what I have tried to do is to create and object which contains all the columns and rows that I need at some point and binding this object to the ItemsSource property. Since I have worked with DataGridViews in WindowsForms I have in mind something like this:

DataTable myTable = new DataTable();

DataColumn col01 = new DataColumn("col 01");
myTable.Columns.Add(col01);
DataColumn col02 = new DataColumn("col 02");
myTable.Columns.Add(col02);

DataRow row = myTable.NewRow();
row[0] = "data01";
row[1] = "data02";
myTable.Rows.Add(row);

row = myTable.NewRow();
row[0] = "data01";
row[1] = "data02";
myTable.Rows.Add(row);

但是我一直无法在 WPF 中找到一种方法来做同样的事情,因为我需要一些列作为 DataGridComboBoxColumns 例如.

But I haven't been able to find a way to do the same thing in WPF since I need some columns to be DataGridComboBoxColumns for example.

实际上我在这个网站上读过很多关于它的帖子,但没有一个对我有帮助.我真的迷路了.

Actually I have read many post about it in this site, but none of them helped to me. I am really lost.

有人可以帮我吗?我只需要能够创建一个可能包含 DataGridTextColumns 或 `DataGridComboBoxColumns 等的表,以便将这个最终对象绑定到 DataGrid 的 ItemsSource 属性.

Could anyone help me? I just need to be able to create a table which may contain DataGridTextColumns or `DataGridComboBoxColumns, etc, In order to bind this final object to the DataGrid's ItemsSource property.

希望有人能帮助我.

推荐答案

好吧,我举个和你需求差不多的例子

Okay, let me try to take an example which is similar to your needs

假设我们使用这个类:

public class MyObject
{
   public int MyID;
   public string MyString;
   public ICommand MyCommand;
}

并且我们愿意显示一个 DataGrid 列出 ID,并将 Button 作为第二列,属性 MyString 作为内容,点击后会启动 ICommand MyCommand,它会在任何你想要的新窗口中打开.

And we are willing to display a DataGrid listing the ID, and having as a second column a Button, with the property MyString as content, which, when clicked, launches the ICommand MyCommand which opens in a new window whatever you want.

以下是您应该在视图方面拥有的内容:

Here is what you should have on the View side:

    <DataGrid ItemsSource="{Binding MyList}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="ID" Binding="{Binding MyID}" />
            <DataGridTemplateColumn Header="Buttons">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="{Binding MyString}" Command="{Binding MyCommand}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

这将显示一个 DataGrid 获取名为MyList"的 IEnumerable 中的所有内容,并显示之前定义的两列.

This will show a DataGrid taking all the content in an IEnumerable<MyObject> named 'MyList', and shows two columns as defined before.

现在,如果您需要定义命令.首先,我建议您阅读这个 MVVM 的介绍性链接并获取RelayCommand 类(这就是我们要用于解决您的问题的类)

Now if you need to define the command. First, I recommend you read this introductory link to MVVM and take the RelayCommand class (that's what we're gonna use for your problem)

因此,在定义 MyListViewModel 中,您应该如何定义一些有用的对象:

So, in your ViewModel, the one which defines the MyList, here is how you should define some of the useful objects:

public ObservableCollection<MyObject> MyList { get; set; }

// blah blah blah

public void InitializeMyList()
{
  MyList = new ObservableCollection<MyObject>();
  for (int i = 0; i < 5; i++)
  {
    MyList.Add(InitializeMyObject(i));
  }
}

public MyObject InitializeMyObject(int i)
{
  MyObject theObject = new MyObject();
  theObject.MyID = i;
  theObject.MyString = "The object " + i;
  theObject.MyCommand = new RelayCommand(param =< this.ShowWindow(i));
  return theObject
}

private void ShowWindow(int i)
{
  // Just as an exammple, here I just show a MessageBox
  MessageBox.Show("You clicked on object " + i + "!!!");
}

这篇关于创建自定义 DataGrid 的 ItemsSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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