Binding Wpf Datagrid Cell Background Colour with Trigger

Binding WPF Datagrid cell background colour with trigger

You need to set CellStyle to target DataGridCell instead of only TextBlock.

If you want this dataTrigger to be applied for all cells in your dataGrid, set style on DataGrid CellStyle otherwise you can do that on specific DataGridTextColumn CellStyle as well.

DataGrid

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

Setting datagrid cell background colour wpf

That is happening, because the Content of DataGridCell is a TextBlock (Or a TextBox when the cell is editing). So even an empty cell contains an empty TextBlock (TextBox), i.e. HasContent property never becomes false.

This should work if you do not use custom template with your cells:

<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Content.Text, RelativeSource={RelativeSource Self}}" Value="" >
<Setter Property="Background" Value="Gray"/>
</DataTrigger>
</Style.Triggers>
</Style>

Binding datagrid cell background color

You should use a CellStyle to set the background for the cells:

<DataGridTextColumn MinWidth="50" Header="Count" Binding="{Binding ItemCount}" IsReadOnly="True" CanUserSort="False">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="{Binding Color}"/>
</Style>
</DataGridTextColumn.CellStyle>
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

How is it possible to change datagrid cell background color when is selected and focused?

You have to create and set an EditingElementStyle for your data grid column, because you are in edit-mode when you double click a cell. In that mode, the data grid cell contains specific controls for editing, like a TextBox for text columns, so changing the cell background will not have an effect.

The editing style below sets the Background and Foreground of the TextBox in edit-mode.

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding DataGridRows}" ...>
<DataGrid.Resources>
<!-- ...other data grid resources. -->
<Style x:Key="DataGridTextColumnEditingStyle"
TargetType="{x:Type TextBox}"
BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Background" Value="#FF333333"/>
<Setter Property="Foreground" Value="White"/>
</Style>
</DataGrid.Resources>
<!-- ...other data grid code. -->
<DataGrid.Columns>
<!-- ...other data grid columns -->
<DataGridTextColumn Header="CSV Column"
IsReadOnly="False"
Binding="{Binding Path=CSVColumnValue}"
Width="*"
Foreground="White"
EditingElementStyle="{StaticResource DataGridTextColumnEditingStyle}"/>
</DataGrid.Columns>
</DataGrid>

WPF Change datagrid cell background color using a converter

  1. Return a Brush:

    if (date1.Date > date2.Date)
    {
    return System.Windows.Media.Brushes.Brown;
    }
  2. Return System.Windows.Data.Binding.DoNothing.

WPF/MVVM/XAML DataGridView - setting cell background color based on cell value

Try this Way by giving DataGridTextColumn Cell style

<DataGridTextColumn  Binding="{Binding Active}"
Header="Active"
Width="Auto">
<DataGridTextColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="{ Binding Active, Converter={ StaticResource activeInactiveBackgroundColorConverter }}"/>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>

c#: Datagrid: Changing Foreground Color of Selection by DataTriggers

You should add handling of IsSelected for DataGridCell as well (in the same way with DataGridRow)

<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="Black" />
<Setter Property="Background" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>


Related Topics



Leave a reply



Submit