WPF从对象的数据表绑定到DataGrid [英] WPF Binding to a DataGrid from a DataTable of Objects

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

问题描述

我对Databinding还是陌生的,并且已经阅读和研究这个问题了几个小时了,我希望有人至少可以指出我正确的方向。

I'm still new to Databinding and have been reading and researching this question for hours now, and I'm hoping someone can at least point me in the right direction.

我所拥有的是一个数据表,其中填充了以下对象:

What I have is a DataTable populated with the objects such as:

public class SimpleObject
{

    public string DisplayValue { get; set; }
    public bool Match { get; set; }
    public string BackGroundColor
    {
        get { if (Match) return "Green"; else return "White"; }
        set { //do nothing }
    }

}

我已经为数据表的列设置了标题,如下所示:

I've set up my headers for the columns of the datatable as such:

DataTable MyDataTable = new DataTable()
List headers = new List<string>() {"Header1", "Header2", "Header3", "Header4"}
foreach (string key in headers)
{         
    MyDataTable.Columns.Add(new DataColumn(key, typeof(SimpleObject)));
}

然后通过添加类似于以下内容的行填充我的DataTable行:

And populated my DataTable rows by adding rows similar to:

SimpleObject[] rowList = new SimpleObject[4]
DataRow dataRow = MyDataTable.NewRow();
for(int i = 0; i < 4; i++)
{
    //Not really how I determine values, but this will do for a basic example
    rowList[i].DisplayValue = i.ToString();
    rowList[i].Match = i % 2 == 0;
}
dataRow.ItemArray = rowList;
MyDataTable.Rows.Add(dataRow);

SimpleDataGrid.DataContext = MyDataTable;

现在,我想做什么是将MyDataTable绑定到DataGrid,例如

Now, what I want to do is bind MyDataTable to the DataGrid such that:


  • SimpleObject.DisplayValue显示在单元格的值中

  • 单元格的背景色由SimpleObject.BackGroundColor
  • 确定

如果有人可以给我有关如何操作的建议,我将不胜感激!到目前为止,我已经尝试做类似的事情:

If anyone could give me advice as to how to this, I would much appreciate it! So far I've tried doing something similar to this:

 <DataGrid Name="SimpleDataGrid" 
                      AutoGenerateColumns="False"
                      ItemsSource="{Binding}">
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="Header1" Width="100">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Path=Header1.DisplayValue}"
                                           BackGround="{Binding Path=Header1.BackGroundColor}"
                                   />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>

 </DataGrid>

但是没有运气。我什至无法将DisplayValue绑定到Text(即使不尝试绑定BackGroundColor)。任何帮助或指示将不胜感激!

But had no luck. I can't even get the DisplayValue to bind to the Text (even without trying to bind BackGroundColor). Any help or direction would be greatly appreciated!

推荐答案

我尝试复制您的问题,但对我来说似乎很好。

I have tried replicating your problem, but it seems to work fine for me.

我在 DataGrid 中添加了2列,并且 DataTable 中添加了1个项目,因此将创建1行。

I have added 2 columns to the DataGrid, and the DataTable has 1 item added to it, so 1 row will be created.

TextBlocks 都具有 Text Background 属性正确绑定。

The TextBlocks have both the Text and Background properties bound correctly.

这是我使用的源代码。

Here is the the source code I have used. See if it works for you.

XAML:

<DataGrid Name="SimpleDataGrid" 
                  AutoGenerateColumns="False"
                  ItemsSource="{Binding}">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Header1" Width="100">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Header1.DisplayValue}"
                               Background="{Binding Path=Header1.BackGroundColor}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Header2" Width="100">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Header2.DisplayValue}"
                               Background="{Binding Path=Header2.BackGroundColor}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

代码隐藏:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        DataTable MyDataTable = new DataTable();
        List<string> headers = new List<string>() { "Header1", "Header2", "Header3", "Header4" };

        foreach (string key in headers)
        {         
            MyDataTable.Columns.Add(new DataColumn(key, typeof(SimpleObject)));
        }

        SimpleObject[] rowList = new SimpleObject[4];
        DataRow dataRow = MyDataTable.NewRow();

        for (int i = 0; i < 4; i++)
        {
            //Not really how I determine values, but this will do for a basic example
            rowList[i] = new SimpleObject();
            rowList[i].DisplayValue = i.ToString();
            rowList[i].Match = i % 2 == 0;
        }
        dataRow.ItemArray = rowList;
        MyDataTable.Rows.Add(dataRow);

        SimpleDataGrid.DataContext = MyDataTable;
    }        
}

public class SimpleObject
{
    public string DisplayValue { get; set; }
    public bool Match { get; set; }
    public string BackGroundColor
    {
        get 
        { 
            if (Match) 
                return "Green"; 
            else return "Blue"; 
        }
    }
}

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

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