在WPF中打开ContextMenu时保留DataGrid IsSelectionActive? [英] Retain DataGrid IsSelectionActive when a ContextMenu opens in WPF?

查看:263
本文介绍了在WPF中打开ContextMenu时保留DataGrid IsSelectionActive?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataGrid ,其样式为 IsSelectionActive ;然而,一旦 ContextMenu 打开,网格会失去 IsSelectionActive ,并且它看起来像用户,上下文菜单以某种方式采取了选择,可能会混淆用户。

I have a DataGrid which has a style for IsSelectionActive; however, as soon as the ContextMenu opens, the grid loses IsSelectionActive and it looks like to the user that as if the context menu somehow took the selection and may confuse the user.

有一种方法可以保留 IsSelectionActive

<ControlTemplate.Triggers>
    <MultiTrigger>
        <MultiTrigger.Conditions>
            <!--<Condition Property="Selector.IsFocused" Value="True" />-->
            <Condition Property="IsSelected" Value="True" />
        </MultiTrigger.Conditions>

        <Setter Property="Background" Value="Red" />
    </MultiTrigger>

    <MultiTrigger>
        <MultiTrigger.Conditions>
            <Condition Property="Selector.IsFocused" Value="False" />
            <Condition Property="IsSelected" Value="False" />
        </MultiTrigger.Conditions>

        <Setter Property="Background" Value="Green" />
    </MultiTrigger>

    <MultiTrigger>
        <MultiTrigger.Conditions>
            <Condition Property="Selector.IsFocused" Value="False" />
            <Condition Property="IsSelected" Value="True" />
        </MultiTrigger.Conditions>

        <Setter Property="Background" Value="Blue" />
    </MultiTrigger>


推荐答案

这里是我在测试应用程序中使用的整个XAML以获得所需的行为:

Here is an entire XAML that I used in test application to get your desired behavior:

<Window x:Class="DataGridSelectionActive.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DataGridSelectionActive"
        Title="MainWindow" Height="350" Width="525">

    <!-- People is just an ObservableCollection derived class. -->
    <Window.DataContext>
        <local:People/>
    </Window.DataContext>

    <Window.Resources>

        <ContextMenu x:Key="dataGridContextMenu">
            <MenuItem Header="Some context menu item"/>
        </ContextMenu>

        <Style TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                    <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                </Trigger>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="True"/>
                        <Condition Binding="{Binding IsKeyboardFocusWithin, RelativeSource={RelativeSource AncestorType=DataGrid}}" Value="False"/>
                    </MultiDataTrigger.Conditions>
                    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                    <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                </MultiDataTrigger>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="True"/>
                        <Condition Binding="{Binding ContextMenu.IsOpen, RelativeSource={RelativeSource AncestorType=DataGrid}}" Value="True"/>
                    </MultiDataTrigger.Conditions>
                    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                    <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>

    </Window.Resources>

    <DockPanel>
        <!-- Added button for testing keyboard focus. -->
        <Button DockPanel.Dock="Top" Content="Click me"/>
        <DataGrid ItemsSource="{Binding}" ContextMenu="{StaticResource dataGridContextMenu}"/>
    </DockPanel>

</Window>

启用此行为的关键是如果多个触发器冲突 Setters 同时活动,最后一个获胜。

The key thing that enables this behavior is that if multiple triggers that have conflicting Setters are active simultaneously, the last one wins.

这篇关于在WPF中打开ContextMenu时保留DataGrid IsSelectionActive?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆