Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DataGrid style

Tags:

c#

wpf

datagrid

I have a DataGrid with the following style

<Style x:Key="DataGridRowStyle1" TargetType="{x:Type DataGridRow}">
    <Setter Property="Foreground" Value="#FFB3B3B3"/>
    <Setter Property="Height" Value="25"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="Template" Value="{DynamicResource DataGridRowControlTemplate1}"/>
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="#FF262626"/>
        </Trigger>
        <Trigger Property="ItemsControl.AlternationIndex" Value="0">
            <Setter Property="Background" Value="#FF383838"/>
        </Trigger>
        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
            <Setter Property="Background" Value="#FF333333"/>
        </Trigger>
    </Style.Triggers>
</Style>

and it appears like this:

enter image description here

My problem appears when the DataGrid lose the focus:

enter image description here

How can I make it's appearance independent of the focus ?

like image 485
Mahmoud Samy Avatar asked Feb 24 '14 00:02

Mahmoud Samy


1 Answers

Before you try to find a solution, look in your Style/Template of DataGrid, DataGridRow, etc. a StyleTrigger on Focus (IsFocused trigger), because it can not be the default behavior.

If do not have one, try to add EvenTriggers for events GotFocus and LostFocus like this:

<Window.Resources>
    <SolidColorBrush x:Key="GotFocusColor" Color="Green" />
    <SolidColorBrush x:Key="LostFocusColor" Color="Transparent" />

    <Style TargetType="{x:Type DataGridRow}">
        <Setter Property="Foreground" Value="#FFB3B3B3"/>
        <Setter Property="Height" Value="25"/>
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>

        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="#FF262626"/>
            </Trigger>

            <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                <Setter Property="Background" Value="#FF383838"/>
            </Trigger>

            <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                <Setter Property="Background" Value="#FF333333"/>
            </Trigger>

            <EventTrigger RoutedEvent="DataGrid.GotFocus">
                <BeginStoryboard>
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background">
                            <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource GotFocusColor}" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>

            <EventTrigger RoutedEvent="DataGrid.LostFocus">
                <BeginStoryboard>
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background">
                            <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource LostFocusColor}" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
like image 65
Anatoliy Nikolaev Avatar answered Oct 24 '22 01:10

Anatoliy Nikolaev