【问题标题】:How to get IsSelectionActive for WPF DataGrid?如何为 WPF DataGrid 获取 IsSelectionActive?
【发布时间】:2012-06-02 03:46:13
【问题描述】:

我正在尝试让 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 数据网格中不存在。

【问题讨论】:

    标签: c# wpf datagrid css-selectors


    【解决方案1】:

    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>
    

    【讨论】:

    • 就是这样!你能解释一下为什么我们需要使用这个 Selector. 而我们不需要它来处理几个属性吗?
    • @rFactor:该样式的目标是具有依赖属性 IsSelected 的 DataGridCell。没有 IsSelectionActive 属性。 Selector 类(派生自 ItemsControl、ListBox 的基类、DataGrid 等任何支持选择的东西)具有只读附加属性 IsSelectionActive(以及附加属性 IsSelected 等)。所以你需要指定 Selector.IsSelectionActive,就像你使用 Grid.Row="1" 一样,它是 Grid 定义的附加属性,而不是网格中项目的属性。
    猜你喜欢
    • 2012-06-02
    • 2014-12-02
    • 2010-11-09
    • 1970-01-01
    • 2021-12-26
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    相关资源
    最近更新 更多