c# - WPF DataGrid : How do I access the DataRow a TextBox etc. 驻留在?

标签 c# wpf xaml datagrid

所以我有这样定义的列:

                        <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/

相关文章:

c# - 在 C++ 中使用 C# DLL

c# - 如何检测 C# 中 2 个图像之间的旋转角度?

wpf - 如何在WPF中播放Flash?

xaml - LongListSelector 日期分组列表

c# - 如何最好地限制通用 <T> 类中的类型,其中所有类型都派生自同一基类

wpf - 使 Viewbox 垂直缩放但水平拉伸(stretch)

wpf - 设置堆栈面板中所有文本 block 的样式

c# - 如何消除 Switch 语句?

c# - WPF ListView 绑定(bind)到集合

c# - 从表单中的 UserControl,如何访问父表单的 DataGridView?