将customObject绑定到DataGrid [英] Bind customObject to DataGrid

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

问题描述

将对象列表绑定到DataGrid的基础将在此讨论如何绑定List< CustomObject>到WPF DataGrid?



我的设置稍微复杂一点:$ b​​ $ b我有 RowModels 其中包含该行的元数据以及 CellModels 的列表。 CellModels 再次包含dataGrid中显示的一些元数据和属性。



要填充我的DataGrid我设置了

  ItemsSource ={Binding RowModelList,UpdateSourceTrigger = PropertyChanged} 

并通过


$ b逐个映射列$ b

 < DataGridTextColumn Binding ={Binding CellModelList [0] .Value,UpdateSourceTrigger = PropertyChanged}/> 
< DataGridTextColumn Binding ={Binding CellModelList [1] .Value,UpdateSourceTrigger = PropertyChanged}/>
...

这样我也可以为每列指定一个ColumnHeader,并修改



然而,我也想在我的中使用一个 DataTrigger DataGridCell -Style来访问我的CellModels元数据。



这样做,但是..好吧



当然可以创建每个列的样式如

 < Style x:Key =CellStyleColumnZeroTargetType =DataGridCell> 
< Style.Triggers>
< DataTrigger Binding ={Binding CellModelList [0] .Enabled}Value =False>
< Setter Property =IsEnabledValue =False/>
< / DataTrigger>
< /Style.Triggers>
< / Style>

问题:



我需要根据我的 CellModel 中的元数据来设置 DataGridCell 的属性。



我确定有一个更优雅的解决方案(比上图所示),不需要自定义每一列。我仍然需要手动更改单列(特别是 ColumnHeaders ),这就是为什么我首先手动声明DataGridTextColumns一个接一个。 >

您是否有任何建议如何正确处理?

解决方案

在后续问题中回答。



我使用的解决方案是为 DataGridCell 创建一个附加属性,其中我存储CellModel并从那里访问属性。



查看链接的问题代码:
WPF DataGrid的结构 - 根据值更改单元格


The basics of binding a list of objects to a DataGrid are discussed here How do I bind a List<CustomObject> to a WPF DataGrid?.

My setup is slightly more complex: I have RowModels which contain metadata for the row as well as a list of CellModels. The CellModels again contain some metadata and a Value property which is shown in the dataGrid.

To fill my DataGrid I did set

ItemsSource="{Binding RowModelList, UpdateSourceTrigger= PropertyChanged}"

and mapped the columns one-by-one via

<DataGridTextColumn Binding="{Binding CellModelList[0].Value, UpdateSourceTrigger= PropertyChanged}"/>
<DataGridTextColumn Binding="{Binding CellModelList[1].Value, UpdateSourceTrigger= PropertyChanged}"/>
...

This way i could also specify a ColumnHeader for each column and modify the presentation of each column.

However i would like to also use a DataTrigger in my DataGridCell-Style to access the metadata of my CellModels.

this works, but.. well:

Of course I could create a Style for each column like

<Style x:Key="CellStyleColumnZero" TargetType="DataGridCell">
    <Style.Triggers>
        <DataTrigger Binding="{Binding CellModelList[0].Enabled}" Value="False">
            <Setter Property="IsEnabled" Value="False"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

Question:

I need to set Properties of the DataGridCell depending on the metadata in my CellModel.

I am sure there is a more elegant solution (than the one shown above) which doesn't require customizing every single column. I still need to be able to manually change single columns (especially the ColumnHeaders) which is why i manually declared the DataGridTextColumns one-by-one in the first place.

Do you have any suggestions how to approach this properly?

解决方案

This was answered in a followup question.

The solution i used was creating an attached property for DataGridCell in which i store the CellModel and access the properties from there.

See the linked question for code: Structure of WPF DataGrid - change cells depending on value

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

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