当DataGrid失焦时,如何对所选行进行去饱和处理?

时间:2009-10-21 00:53:38

标签: wpf xaml triggers wpftoolkit multidatatrigger

UI控件的常规约定是当父控件处于焦点时选定的文本,项目或行要鲜艳(例如,亮蓝色),而当控件耗尽时,选择性文本,项目或行要用于去饱和/暗淡(例如淡蓝色)重点。

ListView/GridView相比,默认情况下WPF DataGrid控件不遵循此约定。即使同一窗口中的另一个控件明显清晰对焦,所选行也会显得明亮。

我认为这应该只是向DataGridCell添加一个触发器,如果​​{DynamicResource {x:Static SystemColors.ControlBrushKey}}失焦,将背景设置为DataGrid,但我无法理解要检查哪个属性。 IsFocused听起来像我想要的,但这不起作用。 IsMouseCaptured似乎也无效。

这是我尝试的最新触发器:

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
        <Condition
            Binding="{Binding IsSelected}"
            Value="True" />
        <Condition
            Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=wt:DataGrid}, Path=IsFocused}"
            Value="False"/>
    </MultiDataTrigger.Conditions>
    <Setter
        Property="BorderBrush"
        Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
    <Setter
        Property="Background"
        Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
    <Setter
        Property="Foreground"
        Value="Gray" />
</MultiDataTrigger>

如果选择单元格并且父控件未对焦,我该怎么做才能使此触发器调暗单元格?

2 个答案:

答案 0 :(得分:2)

嗯,这是一个怪物,但似乎有效:

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
        <Condition
            Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=wt:DataGridRow}, Path=IsSelected}"
            Value="True" />
        <Condition
            Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=wt:DataGrid}, Path=IsKeyboardFocusWithin}"
            Value="True" />
    </MultiDataTrigger.Conditions>
    <Setter
        Property="BorderBrush"
        Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
    <Setter
        Property="Background"
        Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
    <Setter
        Property="Foreground"
        Value="Black" />
</MultiDataTrigger>
<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
        <Condition
            Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=wt:DataGridRow}, Path=IsSelected}"
            Value="True" />
        <Condition
            Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=wt:DataGrid}, Path=IsKeyboardFocusWithin}"
            Value="False" />
    </MultiDataTrigger.Conditions>
    <Setter
        Property="BorderBrush"
        Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
    <Setter
        Property="Background"
        Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
    <Setter
        Property="Foreground"
        Value="Gray" />
</MultiDataTrigger>

答案 1 :(得分:0)

从我的一些代码中提取:当树视图失焦时,这是树视图项目背景的触发器。

                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="true"/>
                                <Condition Property="IsSelectionActive" Value="false"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Background" TargetName="SelectBorder">
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="#939F90" Offset="0"/>
                                        <GradientStop Color="#BCC4BA" Offset="1"/>
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                        </MultiTrigger>

我相信IsSelectionActive是关键条件......