Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

How to override WPF DataGrid IsSelected Background color by IsMouseOver Background color

$
0
0

Hi,

I am looking for solution for mouseover row background color override selected row background color. I would like to display IsMouseOver background color instead of IsSelected background color when MouseOver on a selected row.

I've tried 2 ways to implement this. Please advise. Thanks.

1. Include RowStyle and CellStlye in DataGrid: (It's only work for Cell. When I mouse over the cell on selected row, the cell background color will change to Pink, row background color remain as Orange)

<DataGrid.RowStyle><Style TargetType="DataGridRow"><Style.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter Property="Background" Value="Pink"/></Trigger><Trigger Property="IsKeyboardFocusWithin" Value="True"><Setter Property="Background" Value="Orange" /></Trigger></Style.Triggers></Style></DataGrid.RowStyle><DataGrid.CellStyle><Style TargetType="DataGridCell"><Style.Triggers><Trigger Property="IsSelected" Value="True"><Setter Property="Background" Value="Orange" /></Trigger><Trigger Property="IsMouseOver" Value="True"><Setter Property="Background" Value="Pink"/></Trigger></Style.Triggers></Style></DataGrid.CellStyle>
2. Use of VisualStateManager. (It's only work for MouseOver)
<Style x:Key="DataGridStyle" TargetType="{x:Type DataGrid}"><Setter Property="Foreground" Value="#FF003658"/><Setter Property="RowStyle" Value="{StaticResource DataGridRowStyle}"/></Style><Style x:Key="DataGridRowStyle" TargetType="DataGridRow"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="DataGridRow"><Border x:Name="DGR_Border" 
                Background="{TemplateBinding Background}"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}"
                SnapsToDevicePixels="True"><VisualStateManager.VisualStateGroups><VisualStateGroup x:Name="CommonStates"><VisualState x:Name="Normal"/><VisualState x:Name="Normal_AlternatingRow"/><VisualState x:Name="Unfocused_Editing"/><VisualState x:Name="Normal_Editing"/><VisualState x:Name="Unfocused_Selected"/><VisualState x:Name="Normal_Selected"><Storyboard Storyboard.TargetName="Highlight"><ColorAnimation Duration="0" Storyboard.TargetProperty="Color" To="Orange"/></Storyboard></VisualState><VisualState x:Name="MouseOver_Unfocused_Editing"/><VisualState x:Name="MouseOver_Editing"/><VisualState x:Name="MouseOver_Unfocused_Selected"/><VisualState x:Name="MouseOver_Selected"><Storyboard Storyboard.TargetName="Highlight"><ColorAnimation Duration="0" Storyboard.TargetProperty="Color" To="Pink"/></Storyboard></VisualState><VisualState x:Name="MouseOver"><Storyboard Storyboard.TargetName="Highlight"><ColorAnimation Duration="0" Storyboard.TargetProperty="Color" To="Yellow"/></Storyboard></VisualState></VisualStateGroup></VisualStateManager.VisualStateGroups><SelectiveScrollingGrid x:Name="selectiveScrollingGrid"><SelectiveScrollingGrid.Background><SolidColorBrush x:Name="Highlight" Color="Transparent"/></SelectiveScrollingGrid.Background><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}"/><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" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/></SelectiveScrollingGrid></Border></ControlTemplate></Setter.Value></Setter></Style>


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>