Search code examples
wpfdatagridresourcedictionary

how can set the ControlBrushKey in a dictionary for the all dataGrids?


I have this dictionary:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="{x:Type StackPanel}">
        <Setter Property="Margin" Value="12,12,20,20"/>
    </Style>

    <Style TargetType="{x:Type DataGrid}">
        <Setter Property="IsReadOnly" Value="true"/>
        <Setter Property="CanUserAddRows" Value="false"/>
        <Setter Property="CanUserDeleteRows" Value="true"/>
        <Setter Property="AutoGenerateColumns" Value="false"/>
        <Setter Property="VerticalScrollBarVisibility" Value="Visible"/>
        <Setter Property="HorizontalScrollBarVisibility" Value="Visible"/>
    </Style>


    <Style TargetType="{x:Type DataGridRow}">
        <Setter Property="Background" Value="Black"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="SkyBlue" />
        </Style.Resources>
    </Style>

In my views, I load the dictionary and the dataGrid get the propertd of the DataGrid, but they don't use the properies of the DataGridRow.

I would like to know how can I set the properties of the DataGridRows, because I would like to change the color of the selected rows when the dataGrid has not the focus because the defaulr grey colours is not seen good.

Thanks.


Solution

  • You can use the DataGridRow IsSelected property in a style trigger as shown below. You can then modify the colour of the selected rows when they are selected.

    <Style TargetType="{x:Type DataGridRow}">
        <Setter Property="Background" Value="SkyBlue"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="SkyBlue" />
        </Style.Resources>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="SkyBlue"/>
                <Setter Property="FontWeight" Value="Bold"/>
                <Setter Property="BorderBrush" Value="Tomato" />
                <Setter Property="BorderThickness" Value="2" />
            </Trigger>
        </Style.Triggers>
    </Style>