【问题标题】:Binding WPF Datagrid cell background colour with trigger使用触发器绑定 WPF Datagrid 单元格背景颜色
【发布时间】:2015-08-29 14:10:57
【问题描述】:

我希望 WPF 数据网格单元格的背景颜色在内容被修改后改变颜色。每个单元格后面都有一个 ViewModel 对象,该对象包含以下属性 - Value、OriginalValue 和 Modified。当用户编辑单元格内容时,这会通过数据绑定自动触发 Amount 属性。然后,此属性设置器将其与原始值进行检查,并将布尔型 Modified 属性分别设置为 true 或 false,通知绑定以更新这些属性。

到目前为止,我已经通过 DataGridTextColumn 的 ElementStyle 属性上的 Style 实现了部分结果,如下所示

<Style x:Key="DataGridTextStyle" TargetType="{x:Type TextBlock}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=MyViewModel.Modified}" Value="True">
            <Setter Property="Background" Value="Yellow"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

这会更新文本内容的背景颜色,但这只是单元格中心的一小块区域。我希望整个单元格更新它的背景颜色,而不仅仅是 textblock 属性。

我可以修改上面的触发器以在可视化树中向上搜索以找到父 DataGridCell 并在其上设置 Background 属性,而不是仅设置当前文本块的背景颜色吗?

【问题讨论】:

  • 看来你需要定义CellTemplate

标签: c# css wpf datagrid


【解决方案1】:

您需要将CellStyle 设置为目标DataGridCell 而不仅仅是TextBlock

如果您希望此 dataTrigger 应用于 dataGrid 中的所有单元格,请在 DataGrid CellStyle 上设置样式,否则您也可以在特定的 DataGridTextColumn CellStyle 上执行此操作。

数据网格

     <DataGrid>
        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding MyViewModel.Modified}"
                                 Value="True">
                        <Setter Property="Background" Value="Yellow"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.CellStyle>
    </DataGrid>

DataGridTextColumn

     <DataGrid>
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}">
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding MyViewModel.Modified}" 
                                         Value="True">
                                <Setter Property="Background" Value="Yellow"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

【讨论】:

  • 我必须设置 &lt;DataGridTextColumn.EditingElementStyle &gt; ... &lt;/DataGridTextColumn.EditingElementStyle&gt; 的样式才能让它工作......
  • 为了完整起见,在里面:&lt;Style TargetType="TextBox" &gt;... &lt;/Style&gt; :)
【解决方案2】:

其他人可能会受益于代码隐藏方法中的这种 WPF“动态数据触发器”

此代码允许用户使用他们想要的指定文本突出显示数据行。

    var st = new Style();
    st.TargetType = typeof(DataGridRow);
    var RedSetter = new Setter( DataGridRow.BackgroundProperty, Brushes.Red);
    var dt = new DataTrigger(){
        Value = CurrentTextToFilter,
        Binding = new Binding("Value")               
    };
    dt.Setters.Add(RedSetter);
    st.Triggers.Add(dt);
    XDG.RowStyle = st;
    PropChanged("MainDataCollection");
  • 参数 CurrentTextToFilter 用户输入到绑定到后面代码的 XAML Textbox.Text 属性中的文本。

  • 变量 XDG 是数据网格 XAML 名称,并且 RowStyle 设置为新样式。

  • 确保将设置器添加到 DataTrigger,如图所示。如果直接将其添加到 Rowstyle 中,所有行都会变为红色。

【讨论】:

    猜你喜欢
    • 2011-12-12
    • 1970-01-01
    • 2013-05-12
    • 2018-09-26
    • 2011-04-03
    • 2017-10-04
    • 2011-10-15
    • 2010-12-17
    • 1970-01-01
    相关资源
    最近更新 更多