简体   繁体   English

如何绑定到DataGrid / DataTable中的具体对象

[英]How to bind to concrete objects in a DataGrid/DataTable

Edit: The columns are known only at runtime, which I why I construct the data in a DataTable on the fly. 编辑:这些列仅在运行时才知道,这就是为什么我要动态地在DataTable中构造数据的原因。

I have some dynamic data which I have to show in a DataGrid. 我有一些动态数据,必须在DataGrid中显示。 Each cell in the datagrid is going to bind on to a complex data type. 数据网格中的每个单元都将绑定到复杂的数据类型。 I construct the DataTable successfully and bind onto it. 我成功构造了DataTable并将其绑定到它。 The DataGrid correctly autogenerates its columns, but all the data are strings. DataGrid可以正确自动生成其列,但是所有数据都是字符串。

In the code behind (for temporary debugging) I am handling the AutoGeneratingColumn event and trying to set the column type dynamically. 在后面的代码中(用于临时调试),我正在处理AutoGeneratingColumn事件,并尝试动态设置列类型。 If have a complex type: 如果具有复杂类型:

public class BoolData 
{
    public bool Value { get; set; }
    public Guid Id{ get; set; }
}

In the code behind for the View, I do this: 在View的背后代码中,我这样做:

private void DataGrid_OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.PropertyType == typeof(string))
    {
        var col = new DataGridTextColumn {Binding = new Binding(e.PropertyName)};
        e.Column = col;
        e.Column.Header = e.PropertyName;
    }
    else if (e.PropertyType == typeof(BoolData))
    {
        var col = new DataGridCheckBoxColumn {Binding = new Binding(e.PropertyName + "." + "Value")};
        e.Column = col;
        e.Column.Header = e.PropertyName;
    }
}

The problem is that even where I have added MyBool objects into the DataTable, the string "MyNamespace.MBoolData" is what is in the DataGrid, meaning that the first "if" clause is the only one used. 问题是,即使我将MyBool对象添加到DataTable中,字符串“ MyNamespace.MBoolData”也是DataGrid中的内容,这意味着第一个“ if”子句是唯一使用的子句。 It's as if either the DataGrid or the DataTable is calling ToString() on the object before attempting to display it. 就像DataGrid或DataTable在尝试显示对象之前在对象上调用ToString()一样。

So I guess my question is - how to I create a dynamic DataTable containing complex objects, then bind to it. 所以我想我的问题是-如何创建一个包含复杂对象的动态DataTable,然后绑定到它。

You just need to define your columns in XAML and use a DataGridTemplateColumn to define what your complex object should look like. 您只需要在XAML中定义列,并使用DataGridTemplateColumn定义复杂对象的外观即可。 There is a full code example on the linked page, but this is the basics... you define a DataTemplate to describe exactly what the complex object should look like in the UI and then you can set that as the value for the DataGridTemplateColumn.CellTemplate property. 链接页面上有完整的代码示例,但这是基础知识。您定义DataTemplate来确切描述UI中复杂对象的外观,然后可以将其设置为DataGridTemplateColumn.CellTemplate的值。属性。

You should try this directly in your XAML. 您应该直接在XAML中尝试此操作。

<DataGrid ItemsSource="{Binding YourDataSource}" AutoGenerateColumns="False" >
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="TheHeaderNameOfYourColumn" Width="SizeToCells">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding Value}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

Without knowing the column name, you should create a template in your DataGrid resource 在不知道列名的情况下,应该在DataGrid资源中创建一个模板

<DataGrid>
    <DataGrid.Resources>
        <DataTemplate DataType="{x:Type my:BoolData}"> <!--where "my" points to the correct namespace-->
            <CheckBox IsChecked="{Binding Value}"/>
        </DataTemplate>
    </DataGrid.Resources>
</DataGrid>

OK, I figured out what the issue was. 好的,我知道了问题所在。 If I had pasted in the code for the creation of the DataTable someone would likely have spotted it. 如果我粘贴了用于创建DataTable的代码,则可能会发现它。

When you're creating your DataTable, specifically your DataColumns, you must explicitly set the DataType, else it defaults to string. 创建数据表(特别是数据列)时,必须显式设置数据类型,否则默认为字符串。 Not at all obvious, but finally got it working. 一点也不明显,但是终于让它起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM