wpf - 后台的 DataGridRow MultiDataTrigger : IsSelected condition not working

标签 wpf datagrid triggers background datatrigger

我有一个绑定(bind)到集合的数据网格。我已将以下行样式应用于数据网格:

<DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Status}" Value="Down">
                <Setter Property="Background" Value="Red" />
                <Setter Property="Foreground" Value="White" />
            </DataTrigger>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding Status}" Value="Down" />
                    <Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsSelected}" Value="True" />
                </MultiDataTrigger.Conditions>
                <Setter Property="Background" Value="Green" />
                <Setter Property="Foreground" Value="White" />
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.RowStyle>

数据触发器正在工作,但多重数据触发器不起作用。失败的条件是检查 datagridrow 上的 IsSelected 是否为 true。如果我更改它,使其使用始终返回 true 的值转换器,我将看到行显示为绿色。

基本上,我希望数据对象的 status 属性设置为红色的行,但是当我选择这些行时,它们需要为绿色(或实际上为深红色),而不是默认的蓝色。

最佳答案

您的绑定(bind)可能非常好。但 DataGrid 使用 SystemColors.HighlightBrush 和 SystemColors.HighlightTextBrush 来突出显示选定的行。要更改所选行的颜色,您只需覆盖 DataGrid 的画笔,如下所示:

    <DataGrid.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
    </DataGrid.Resources>

这样您应该能够看到触发器设置的背景颜色。

希望这有帮助!

关于wpf - 后台的 DataGridRow MultiDataTrigger : IsSelected condition not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5763749/

相关文章:

c# - 添加 .resx 文件时 WPF .net Core 3.0 编译错误

wpf - WPF DataGridTemplateColumn。我想念什么吗?

wpf - 如何将数据网格中的单元格内容居中?

postgresql - 错误触发器。触发不返回。数据库

mysql - 当发生变化时更新 mySQL 表

c# - WPF 绑定(bind) RelativeSource

c# - WPF 绑定(bind) ObservableCollection<string> 到 ItemsControl 不起作用

wpf - DataGridCheckBoxColumn 立即绑定(bind)

c# - WPF EF6 Datagrid Cell 在以编程方式更改时不会更新,除非用户编辑单元格

mysql - ActiveRecord 回调还是 MySql 触发器?