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

查看:1733
本文介绍了创建一个自定义的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

我们假设我们使用这个类:

Let's assume we use this class:

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

我们愿意显示一个 DataGrid 列出ID,并将属性 MyString 作为内容作为第二列 Button ,当被点击时,启动在新窗口中打开的 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.

这是你在View方面应该有的:

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 IEnumerable< MyObject> 中的内容命名为MyList,并显示了之前定义的两列。

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)

所以,在您的 ViewModel ,定义 MyList 的那个,这里是如何定义一些有用的对象: p>

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天全站免登陆