1

UI 控件的常规约定是,当父控件处于焦点时,选定的文本、项目或行颜色鲜艳(例如,亮蓝色),而当控件失焦时,将其不饱和/暗淡(例如,淡蓝色)。

与 相比ListView/GridView,WPFDataGrid控件默认不遵循此约定。即使同一窗口中的另一个控件清晰地聚焦,选定的行也会显得明亮。

我认为这应该只是添加一个触发器来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>

当单元格被选中并且父控件不在焦点上时,我该怎么做才能使此触发器使单元格变暗?

4

2 回答 2

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>
于 2009-10-21T02:03:51.573 回答
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 是关键条件......

于 2009-10-21T01:10:51.417 回答