简体   繁体   中英

WPF DataGridRow IsSelected Triggers

I Am looking to change the selected style of the rows in my DataGrid away from that default dark blue and white text to actually depend upon the existing Foreground color in the row like this:

<MultiDataTrigger>
  <MultiDataTrigger.Conditions>
    <Condition Property="Foreground" Value="Navy" />
    <Condition Property="IsSelected" Value="True" />
  </MultiDataTrigger.Conditions>
  <MultiDataTrigger.Setters>
    <Setter Property="Background" Value="LightSkyBlue" />
  </MultiDataTrigger.Setters>
</MultiDataTrigger>

<MultiDataTrigger>
  <MultiDataTrigger.Conditions>
    <Condition Property="Foreground" Value="Red" />
    <Condition Property="IsSelected" Value="True" />
  </MultiDataTrigger.Conditions>
  <MultiDataTrigger.Setters>
    <Setter Property="Background" Value="LightGoldenrodYellow" />
  </MultiDataTrigger.Setters>
</MultiDataTrigger>

For other controls I am able to find a ControlTemplate that contains triggers that set the selected style, but I cannot find the proper template for a DataGridRow. Does anyone know what template that is set in?

The following is a DataGridRow style I use, to get away from the default blue - maybe it provides the answer you seek?

In your DataGrid, set the RowStyle property to this style.

<Style x:Key="BetterHighlightedDataGridRowStyle" TargetType="sdk:DataGridRow">
    <Setter Property="IsTabStop" Value="False"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="sdk:DataGridRow">
                <sdk:DataGridFrozenGrid x:Name="Root">
                    <sdk:DataGridFrozenGrid.Resources>
                        <Storyboard x:Key="DetailsVisibleTransition">
                            <DoubleAnimation Duration="00:00:0.1" Storyboard.TargetProperty="ContentHeight" Storyboard.TargetName="DetailsPresenter"/>
                        </Storyboard>
                    </sdk:DataGridFrozenGrid.Resources>
                    <sdk:DataGridFrozenGrid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition/>
                    </sdk:DataGridFrozenGrid.ColumnDefinitions>
                    <sdk:DataGridFrozenGrid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </sdk:DataGridFrozenGrid.RowDefinitions>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="NormalAlternatingRow">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundRectangle"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" To=".5" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundRectangle"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="NormalSelected">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundRectangle"/>
                                    <ColorAnimation Duration="0" To="#DDE9EE33" Storyboard.TargetProperty="(Fill).Color" Storyboard.TargetName="BackgroundRectangle"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="MouseOverSelected">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundRectangle"/>
                                    <ColorAnimation Duration="0" To="#E1C8C864" Storyboard.TargetProperty="(Fill).Color" Storyboard.TargetName="BackgroundRectangle"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="UnfocusedSelected">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundRectangle"/>
                                    <ColorAnimation Duration="0" To="#DDE9EE33" Storyboard.TargetProperty="(Fill).Color" Storyboard.TargetName="BackgroundRectangle"/>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="ValidationStates">
                            <VisualState x:Name="Valid"/>
                            <VisualState x:Name="Invalid">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Visibility" Storyboard.TargetName="BackgroundRectangle">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="InvalidVisualElement"/>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Rectangle x:Name="BackgroundRectangle" Grid.ColumnSpan="2" Fill="#FFBADDE9" Opacity="0" Grid.RowSpan="2"/>
                    <Rectangle x:Name="InvalidVisualElement" Grid.ColumnSpan="2" Fill="#FFF7D8DB" Opacity="0" Grid.RowSpan="2"/>
                    <sdk:DataGridRowHeader x:Name="RowHeader" sdk:DataGridFrozenGrid.IsFrozen="True" Grid.RowSpan="3"/>
                    <sdk:DataGridCellsPresenter x:Name="CellsPresenter" Grid.Column="1" sdk:DataGridFrozenGrid.IsFrozen="True"/>
                    <sdk:DataGridDetailsPresenter x:Name="DetailsPresenter" Grid.Column="1" Grid.Row="1"/>
                    <Rectangle x:Name="BottomGridLine" Grid.ColumnSpan="2" Fill="Black" HorizontalAlignment="Stretch" Height="2" Grid.Row="2"/>
                </sdk:DataGridFrozenGrid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

If you need only change background color for selected row in DataGrid you should achieve this with style for DataGridCell.

    <Style TargetType="{x:Type DataGridCell}">
        <Style.Triggers>
            <Trigger Property="DataGridCell.IsSelected"
                     Value="True">
                <Setter Property="Background"
                        Value="LightGreen" />

                <Setter Property="Foreground"
                        Value="Black" />
            </Trigger>
        </Style.Triggers>
    </Style>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM