In my WPF document, I have a DataGrid
:
<DataGrid>
<DataGrid.ColumnHeaderStyle >
<Style BasedOn="{StaticResource MaterialDesignDataGridColumnHeader}" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="FontSize" Value="11"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.RowStyle>
<Style BasedOn="{StaticResource MaterialDesignDataGridRow}" TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="Blue" />
<Setter Property="BorderBrush" Value="Blue" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Blue" />
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Background" Value="Red" />
</Trigger>
</Style.Triggers>
<Setter Property="ContextMenu" Value="{StaticResource TasksMenu}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridRow}">
<Border x:Name="DGR_Border" BorderBrush="Transparent" BorderThickness="0" Margin="0,15,0,0" Background="{TemplateBinding Background}" SnapsToDevicePixels="True" Height="50" CornerRadius="10">
<Grid Height="auto" Width="auto">
<SelectiveScrollingGrid>
<SelectiveScrollingGrid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</SelectiveScrollingGrid.ColumnDefinitions>
<SelectiveScrollingGrid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</SelectiveScrollingGrid.RowDefinitions>
<DataGridCellsPresenter Grid.Column="1" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" FontSize="5"/>
<DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}"/>
<DataGridRowHeader Grid.RowSpan="2" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" FontSize="5" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
</SelectiveScrollingGrid>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTemplateColumn x:Name="THISISTHECOLUMN" Width="*" MaxWidth="60">
<DataGridTemplateColumn.HeaderStyle>
<Style BasedOn="{StaticResource MaterialDesignDataGridColumnHeader}" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="FontSize" Value="11"/>
</Style>
</DataGridTemplateColumn.HeaderStyle>
<DataGridTemplateColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="Red" />
<Setter Property="BorderBrush" Value="Red" />
</Trigger>
</Style.Triggers>
<Setter Property="Padding" Value="20"/>
<!--<Setter Property="Height" Value="57"/>-->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DataGridCell">
<Border Margin="2" BorderThickness="2" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" CornerRadius="5">
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<!--<Setter Property="BorderBrush"><Setter.Value><MultiBinding><MultiBinding.Converter><local:CellForegroundMultiValueConverter /></MultiBinding.Converter><Binding RelativeSource="{RelativeSource Self}" Path="Column.Header"/><Binding/><Binding Path="HasChanges" /></MultiBinding></Setter.Value></Setter>-->
</Style>
</DataGridTemplateColumn.CellStyle>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Border Background="Blue" Height="40" CornerRadius="10" BorderBrush="Transparent">
<TextBlock Margin="5 0" FontSize="15" Width="auto" Text="1" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Yellow"/>
</Border>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Header 1" Binding="{Binding Header1, UpdateSourceTrigger=PropertyChanged}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="8" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="Header 2" Binding="{Binding Header2, UpdateSourceTrigger=PropertyChanged}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="8" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="Header 3" Binding="{Binding Header3, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="8" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="Blue" />
<Setter Property="BorderBrush" Value="Blue" />
</Trigger>
</Style.Triggers>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Padding" Value="20"/>
<Setter Property="FontSize" Value="9"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Foreground">
<Setter.Value>
<MultiBinding>
<MultiBinding.Converter>
<local:CellForegroundMultiValueConverter />
</MultiBinding.Converter>
<Binding RelativeSource="{RelativeSource Self}" Path="Column.Header"/>
<Binding/>
<Binding Path="HasChanges" />
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</DataGrid.CellStyle>
</DataGrid>
On button hover, the DataGridColumn
changes from Red to Blue perfectly as intended, but when I click on it, the first DataGridTemplateColumn
(labelled THISISTHECOLUMN
) does not change colour. How can I make it so that when the main DataGridColumn
is clicked, it also changes colour?
This is how it looks like so far:
Add a trigger to the DataGridRow
style to change the DGR_Border
border color on selection to Blue
.
<Style BasedOn="{StaticResource MaterialDesignDataGridRow}" TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<!-- ...your other triggers. -->
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Blue" />
</Trigger>
</Style.Triggers>
<!-- ...rest of the style. -->
</Style>
Change the DataGridCell
triggers to set a Blue
Background
and BorderBrush
, instead of Red
.
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="Blue" />
<Setter Property="BorderBrush" Value="Blue" />
</Trigger>
</Style.Triggers>
<!-- ...your other setters. -->
</Style>