在DataGrid中更改突出显示文本笔刷键

时间:2018-10-15 22:36:02

标签: c# wpf

我正在尝试更改数据网格中所选行的突出显示文本笔刷键。我已经尝试了其他问题的许多答案,但没有任何帮助。 我通过数据触发器将行的前景色和背景色设置为不同的颜色。现在,当选择一行时,我想保留前景色和背景色。当我将高光笔刷键设置为透明时,背景色就是我想要的颜色。但是,如果我也将突出显示文本笔刷键设置为透明,则文本将完全消失。有可能改变吗?我尝试了不同的触发方式,但是没有成功。如何更改不同情况下的突出显示颜色(以xaml或最好在后面的代码中),而不将它们全部设置为一种特定颜色?

示例如何设置背景和背景:

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}"
        <Style.Triggers>
            <DataTrigger Binding="{Binding MyData}" Value="MyValue">
                <Setter Property="Foreground" Value="Black"></Setter>
                <Setter Property="Background" Value="LightBlue"></Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding MyData}" Value="MyValue">
                <Setter Property="Foreground" Value="Green"></Setter>
                <Setter Property="Background" Value="Transparent"></Setter>
                <Setter Property="FontWeight" Value="Bold"></Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.RowStyle>

我如何更改突出显示颜色:

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

1 个答案:

答案 0 :(得分:0)

已经有一段时间了,但是我不敢相信我想要这样的东西。将属性值设置为默认值,然后当匹配触发器值时,触发器将更改默认值。

<DataGrid.RowStyle>
   <Style TargetType="{x:Type DataGridRow}">
      <Setter Property="Foreground" Value="Black"></Setter>
      <Setter Property="Background" Value="LightBlue"></Setter>
      <Style.Triggers>
          <DataTrigger Binding="{Binding MyData}" Value="MyValue">
             <Setter Property="Foreground" Value="Green"></Setter>
             <Setter Property="Background" Value="White"></Setter>
             <Setter Property="FontWeight" Value="Bold"></Setter>
          </DataTrigger>
      </Style.Triggers>
   </Style>
</DataGrid.RowStyle>
相关问题