如何将文本框SelectionBrush属性应用于列表框项?

时间:2014-05-23 03:25:30

标签: c# wpf

与使用默认高亮笔刷相反,我希望所选列表框项目的颜色与文本框中所选文本的颜色相同。我认为这将是一个x:Reference,但是如果我需要在控件模板中执行此操作会触发样式元素中的级别?

<Style x:Key="{x:Type ListBoxItem}" TargetType="ListBoxItem">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <Border Name="Border" Padding="2" SnapsToDevicePixels="true">
                    <ContentPresenter />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter TargetName="Border" Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

选中触发器的setter值是我想要的等同于文本选择画笔。

1 个答案:

答案 0 :(得分:1)

在我看来,你几乎就在那里。根据{{​​3}}属性判断,选择画笔的默认Opacity为0.4,因此我们必须模拟:

<Trigger Property="IsSelected" Value="True">
    <Setter TargetName="Border" Property="Background">
        <Setter.Value>
            <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.HighlightColorKey}}" 
                             Opacity="0.4" />
        </Setter.Value>                                
    </Setter>
</Trigger>

我测试了它,它看起来与TextBox中的选定文字背景完全相同。