所以我有这样定义的列:
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=COLUMN_NAME, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
TextBox 不是列的 DataTemplate 中可能存在的唯一控件,我还有 DateTimePickers、ComboBoxes 等。
我想做的是定义一些像这样的样式触发器来访问 DataRow 中的某些属性:
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}},
Path=Row.RowState}" Value="Modified">
<Setter Property="Foreground" Value="LightGreen" />
</DataTrigger>
</Style.Triggers>
</Style>
现在不幸的是,TextBoxes 似乎从来都不是 DataGridRows 的逻辑子项。那么解决方法是什么?当然,我可以创建一个针对 DataGridRows 本身的触发器,但这是多余的,因为设置前景属性不会执行任何操作(TextBoxes 和其他控件位于前面)。
非常感谢任何帮助。 河豚
编辑:这是我选择的解决方案,因为 RowState 不通知它的监听器并且扩展 DataRow 是有问题的。
1) 将事件绑定(bind)到控件,以便在控件的数据发生变化时使用。 2) 刷新 Style 以再次检查 RowState:
Style s = ((TextBox)sender).Style;
((TextBox)sender).Style = null;
((TextBox)sender).Style = s;
显然,它会比这更通用。
编辑 2:这显然行不通,因为我需要单独重置每个控件的样式,即使可能这样做也是一件坏事
最佳答案
请参阅以下内容:
https://stackoverflow.com/questions/4947918/wpftoolkit-datagrid-highlight-modified-rows
就绑定(bind)而言,Row
不是 DataGridRow
的属性,因此您必须使用 DataContext.Row.RowState
。
话虽如此,WPF 无法检测到 RowState
属性的更改,因此您最好的选择可能是将项目包装在 View 模型中,而不是直接绑定(bind)到 DataTable。
关于c# - WPF DataGrid : How do I access the DataRow a TextBox etc. 驻留在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6388133/