user2088807
user2088807

Reputation: 1408

DatagridRow's mouseover not working properly

I have the following styles for my datagrid :

<Style x:Key="StyleDataGrid" TargetType="{x:Type DataGrid}">
        <Setter Property="SelectionMode" Value="Single" />
        <Setter Property="SelectionUnit" Value="FullRow" />
        <Setter Property="CanUserAddRows" Value="False" />
        <Setter Property="AutoGenerateColumns" Value="False" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="CanUserResizeColumns" Value="True" />
        <Setter Property="GridLinesVisibility" Value="Horizontal" />
        <Setter Property="HorizontalGridLinesBrush" Value="Black" />
        <Setter Property="CanUserReorderColumns" Value="False" />
        <Setter Property="HeadersVisibility" Value="Column" />
        <Setter Property="CanUserDeleteRows" Value="False" />
        <Setter Property="Padding" Value="8"/>
    </Style>
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="Padding" Value="5" />
        <Setter Property="BorderBrush" Value="Transparent" />
        <Setter Property="FontSize" Value="14" />
        <Setter Property="FontFamily" Value="Helvetica" />
        <Setter Property="Foreground" Value="Black"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <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}">
                            <ContentPresenter.ContentTemplate>
                                <DataTemplate>
                                    <TextBlock Background="Transparent" Name="text" TextTrimming="CharacterEllipsis"
                                Height="auto" Width="auto" Text="{Binding Text}"/>
                                </DataTemplate>
                            </ContentPresenter.ContentTemplate>
                        </ContentPresenter>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter  Property="Background" Value="Red"/>
                <Setter Property="Foreground" Value="White" />
            </Trigger>
            <Trigger Property="IsMouseOver" Value="True">
                <!--<Setter Property="ToolTip" Value="{Binding Content.Text, RelativeSource={RelativeSource Self}}"/>-->
                <Setter Property="Background" Value="Orange"/>
                <Setter Property="Foreground" Value="White" />
            </Trigger>
        </Style.Triggers>
    </Style>
    <Style TargetType="{x:Type DataGridRow}">
        <Setter Property="Margin" Value="0"/>
        <Setter Property="BorderBrush" Value="Transparent" />
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="Background" Value="{StaticResource CouleurFond}" />
        <Setter Property="Foreground" Value="{StaticResource ResourceKey=CouleurTexte}" />
        <Setter Property="Padding" Value="5"/>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="Red"/>
                <Setter Property="Foreground" Value="White" />
            </Trigger>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Orange"/>
                <Setter Property="Foreground" Value="White" />
            </Trigger>
        </Style.Triggers>
    </Style>

But the mouseOver event on my DataGridRow doesn't work. When My mouse is over a row, the row 's Background is red but the text's foreground remains Black on all my columns excepted the cell under my mouse where the text became white as expected.

But I'd like to have all my line's foreground white when my mouse is over a row. What is wrong with my styles ?

Thank you

Upvotes: 1

Views: 875

Answers (2)

user2088807
user2088807

Reputation: 1408

The trick was to add these lines to manage the mouse over event of my DataGridCell:

<DataTrigger Binding="{Binding Path=IsMouseOver, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" Value="True">
            <Setter Property="Background" Value="{StaticResource ResourceKey=CouleurBoutonHover}"/>
            <Setter Property="Foreground" Value="{StaticResource ResourceKey=CouleurTexteBoutonHover}" />
        </DataTrigger>

and it was working :)

Upvotes: 2

Sheridan
Sheridan

Reputation: 69959

If you comment out your other Styles temporarily, you'll see that actually, your DataGridRow Style works just fine... the Background of the selected DataGridRow is Orange as you required:

<Style TargetType="{x:Type DataGridRow}">
    <Setter Property="Margin" Value="0"/>
    <Setter Property="BorderBrush" Value="Transparent" />
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="Background" Value="{StaticResource CouleurFond}" />
    <Setter Property="Foreground" Value="{StaticResource ResourceKey=CouleurTexte}" />
    <Setter Property="Padding" Value="5"/>
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="Red"/>
            <Setter Property="Foreground" Value="White" />
        </Trigger>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Orange"/>
            <Setter Property="Foreground" Value="White" />
        </Trigger>
    </Style.Triggers>
</Style

Therefore, you need to set your other Styles more carefully. Add them part by part and keep running the program occasionally to check that your problem hasn't reappeared and if it has, just undo the last edit or two, as that was what was causing your problem.


UPDATE >>>

Please read my last paragraph again:

you need to set your other Styles more carefully. Add them part by part and keep running the program occasionally to check that your problem hasn't reappeared and if it has, just undo the last edit or two, as that was what was causing your problem.

Add them part by part does not mean add the whole DataGridCell Style back in one go as you clearly have done. If you had added the setters back into the DataGridCell Style part by part then you would have noticed which setter is causing the problem for you.

Upvotes: 1

Related Questions