Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGrid -- AlternatingRowBackground color interfering with "IsMouseOver" color

I have a DataGrid that uses AlternatingRowBackground to make itself easier to read. In that same grid I also have a background color change for rows based on a "IsMouseOver" Setter Property in my App.xaml file. The problem that I am having is that the rows that have the alternating color (they are not white), do not change to the "IsMouseOver" color when the mouse hovers over it. Basically the AlternatingRowBackground color is taking precedence over my RowStyle. How do I make it so that the colored rows change as well when the mouse hovers over them?

App.xaml:

<!-- DataGrid Row Style -->
    <Style x:Key="RowStyleWithAlternation" TargetType="DataGridRow">
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="Background" Value="GhostWhite"/>
        <Setter Property="FontWeight" Value="Normal"/>
        <Setter Property="ContextMenu" Value="{x:Null}"/>
        <Style.Triggers>
            <Trigger Property="AlternationIndex" Value="1">
                <Setter Property="Background" Value="#FFD0D0E0"/>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Purple"/>
            </Trigger>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="#F9F99F" />
            </Trigger>
        </Style.Triggers>
    </Style>

User Control xaml:

<DataGrid ... AlternatingRowBackground="Gray" RowStyle="{StaticResource RowStyleWithAlternation}" ... />
like image 978
Eric after dark Avatar asked Nov 01 '22 12:11

Eric after dark


1 Answers

It works if you change your UserControl.xaml as follows:

<DataGrid RowStyle="{StaticResource RowStyleWithAlternation}" AlternationCount="2" />

The background is set through the AlternationIndex Trigger on your row which takes no priority over the IsMouseOver.

I've found the answer on this post:

WPF Style Trigger for DataGridRow Background Color Trumped by AlternatingRowBackground Brush

like image 150
Bruno V Avatar answered Nov 15 '22 05:11

Bruno V