如何获取 IsSelectionActive for WPF DataGrid

本文关键字:IsSelectionActive for WPF DataGrid 获取 何获取 | 更新日期: 2023-09-27 18:35:24

我正在尝试让IsSelectionActive使用 WPF DataGrid:

<Style TargetType="{x:Type DataGridCell}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <ContentPresenter />
                <ControlTemplate.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelectionActive" Value="False" />
                            <Condition Property="IsSelected" Value="True" />
                            <!--<Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=IsSelected}" Value="True" />
                            <Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=IsKeyboardFocusWithin}" Value="True" />-->
                        </MultiTrigger.Conditions>

基本上,当网格的焦点丢失,但选择仍然存在时,我想应用一些样式。

遗憾的是,由于某种原因,IsSelectionActive抛出一个错误,指出它在 WPF 数据网格中不存在。

如何获取 IsSelectionActive for WPF DataGrid

IsSelectionActive 是一个附加属性。 我认为你需要使用

<Condition Property="Selector.IsSelectionActive" Value="False" /> 

这种简化的样式对我有用:

<Style x:Key="CellStyle" TargetType="{x:Type DataGridCell}">
    <Style.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="Selector.IsSelectionActive" Value="False" />
                <Condition Property="IsSelected" Value="True" />
            </MultiTrigger.Conditions>
            <Setter Property="Background" Value="Red"/>
        </MultiTrigger>
    </Style.Triggers>
</Style>
相关文章:
  • 没有找到相关文章