如何更改从 DataSet 填充的突出显示的 WPF DataGridCell?

How do you change Highlighted WPF DataGridCell populated from DataSet?

我设法更改了后面代码中突出显示列的颜色。如果选择了该行,则单元格必须采用不同的颜色。如果能在设计器中完成就好了。

It should be a different color when selected

示例如下:

  Dim s As Style = New Style(GetType(DataGridCell))
  s.Setters.Add(New Setter(BackgroundProperty, Brushes.LightBlue))
  s.Setters.Add(New Setter(BorderThicknessProperty, New Thickness(0)))
  s.Setters.Add(New Setter(ForegroundProperty, Brushes.Black))
  s.Setters.Add(New Setter(TextBlock.TextAlignmentProperty, TextAlignment.Right))
  DgwRacunStavke.Columns(DgwRacunStavke.IndexKoloneNaOsnovUNaziva("Može da se upiše u refundaciju")).CellStyle = s

我在设计器中尝试过,但绑定不起作用,因为我可能犯了一个错误:

   <DataGrid.CellStyle> 
    <Style TargetType="DataGridCell" >
     <Style.Triggers>
      <DataTrigger Binding="{Binding Header}" Value="Može da se upiše u refundaciju"/>
     </Style.Triggers>
     <Setter Property="Background" Value="{StaticResource bojaZaIsticanjeKoloneUGridu}"/>
     <Setter Property="BorderBrush" Value="{StaticResource bojaZaIsticanjeKoloneUGridu}"/>
     <Setter Property="Foreground" Value="{StaticResource bojaFontaZaIsticanjeKoloneUGridu}"/>
    </Style>
   </DataGrid.CellStyle>

显然我需要 IsSelected 的触发器...

编辑:解决方案

XAML:

 <Style x:Key="DataGridCell_Isticanje" TargetType="DataGridCell">
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="False">
                <Setter Property="Background" Value="{StaticResource bojaZaIsticanjeKoloneUGridu}"/>
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="Foreground" Value="{StaticResource bojaFontaZaIsticanjeKoloneUGridu}"/>
            </Trigger>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="{StaticResource bojaMouseOver}"/>
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="Foreground" Value="{StaticResource bojaFontaKadaJeSelektovan}"/>
            </Trigger>
        </Style.Triggers>
    </Style>

代码隐藏:

 DGW.Columns(DGW.IndexKoloneNaOsnovUNaziva("Može da se upiše u refundaciju")).CellStyle = FindResource("DataGridCell_Isticanje")

你有没有试过把 <Setter> 放在 </Style.Trigger> 之前它看起来应该是这样的。

<Style.Triggers>
  <DataTrigger Binding="{Binding ElementName=element2, Path=IsActive}" Value="False">
    <Setter Property="Foreground" Value="#787878" />
  </DataTrigger>
  <DataTrigger Binding="{Binding ElementName=element1, Path=IsSelected}" Value="True">
    <Setter Property="Foreground" Value="Aquamarine"/>
    <Setter Property="FontWeight" Value="Bold"/>
  </DataTrigger>
</Style.Triggers>