Mike
Mike

Reputation: 559

WPF DataGrid RowDetailsTemplate focus color

I have a DataGrid with a RowDetailsTemplate. I would like the background color to always match the DataGridTemplateColumn however when focus is moved to a toolbar the focus color is maintained in the DataGridTemplateColumns but lost in the RowDetailsTemplate. How can I maintain the standard focus behavior on a RowDetailsTemplate?

<DataGrid.RowDetailsTemplate>
<DataTemplate>
    <Border
        Margin="0,0,-6,0"
        Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsPresenter}}, Path=ActualWidth}"
        HorizontalAlignment="Left">
        <TextBlock Text="{Binding PreviewMessage}" Padding="32,0,32,8" TextWrapping="Wrap">
            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <Style.Triggers>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding IsSelected}" Value="True" />
                                <Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}, Path=IsKeyboardFocusWithin}" Value="False" />
                            </MultiDataTrigger.Conditions>
                            <MultiDataTrigger.Setters>
                                <Setter Property="Background" Value="{x:Static SystemColors.InactiveSelectionHighlightBrush}" />
                                <Setter Property="Foreground" Value="{x:Static SystemColors.InactiveSelectionHighlightTextBrush}" />
                            </MultiDataTrigger.Setters>
                        </MultiDataTrigger>

                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding IsSelected}" Value="True" />
                                <Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}, Path=IsKeyboardFocusWithin}" Value="True" />
                            </MultiDataTrigger.Conditions>
                            <MultiDataTrigger.Setters>
                                <Setter Property="Background" Value="{x:Static SystemColors.HighlightBrush}" />
                                <Setter Property="Foreground" Value="{x:Static SystemColors.HighlightTextBrush}" />
                            </MultiDataTrigger.Setters>
                        </MultiDataTrigger>

                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
    </Border>
</DataTemplate>

Upvotes: 0

Views: 95

Answers (1)

mm8
mm8

Reputation: 169270

Bind to the IsSelectionActive property instead of the IsKeyboardFocusWithin property:

<Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
    AncestorType={x:Type DataGrid}}, Path=IsSelectionActive}" Value="True" />

Upvotes: 1

Related Questions