Wpf数据网格单元格数据上下文不会保留

本文关键字:数据 保留 上下文 单元格 数据网 网格 Wpf | 更新日期: 2023-09-27 18:19:53

我想手动设置DataGridCell的DataContext,样式如下:

<Style TargetType="DataGridCell" x:Key="DynamicGridCellStyle">               
<Setter Property="DataContext" Value="{x:Null}"/>
</Style>

然后在另一个地方,我正在为列中的所有单元格设置单元格样式:

<DataGridTemplateColumn 
csdpp:CalendarProperties.EnumDay="Tue"
HeaderStyle="{StaticResource DayHeaderStyle}"
CellStyle="{StaticResource DynamicGridCellStyle}">
</DataGridTemplateColumn>

实际上并不是x:Null,而是一些绑定,但这并不重要,x:Null的问题仍然存在。

问题是:在开始调用后,DataGridCell的DataContexts不同于null。有些事情正在改变他们。

有办法抓什么吗?我在寻找DataContextChanged事件,但这只会导致下一个沉重的代码。或者这怎么可能呢?我将DataGrid中的DataGridCell连接到CollectionViewSource,可能有问题吗?CollectionViewSource能否更改DataGridCell DataContext?

Wpf数据网格单元格数据上下文不会保留

最后,我通过向DataGridCell添加附加的依赖属性来存储自定义数据对象,从而解决了这个问题。这个解决方案允许我使用从父代继承的DataGridCell DataContext。