Home > Software design >  How do you change Highlighted WPF DataGridCell populated from DataSet?
How do you change Highlighted WPF DataGridCell populated from DataSet?

Time:11-05

I managed to change the color of the highlighted column in the code behind. If the row is selected, it is necessary that the cell be in a different color. It would be nice if it could be done in a designer.

It should be a different color when selected

The following is an example:

  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

I tried this in a designer but binding doesn't work because I probably made a mistake:

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

Obviously I need a trigger for IsSelected ...

EDIT: Solution

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>

CODE BEHIND:

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

CodePudding user response:

Have you tried to put the <Setter> before the </Style.Trigger> It should look something like this.

<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>
  • Related