更改列表框项目的选择颜色 [英] Change selection color of ListBox Item

查看:94
本文介绍了更改列表框项目的选择颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正常的ListBox,我想将选择颜色更改为红色.这是我到目前为止所得到的.

I have a normal ListBox and I want to change the selection color to Red. Here's what I've got so far.

<Style x:Key="myLBStyle" TargetType="{x:Type ListBoxItem}">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" 
                         Color="red" />
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
                         Color="red" />
    </Style.Resources>
</Style>

正在工作. SelectedItem为红色,即使焦点未对准也保持红色.

It's working. The SelectedItem is Red and stay Red even if it's out of focus.

这是我真正的问题:在我的网格中,我也有一个CheckBox,并且我希望上述样式仅在选中CheckBox时才适用.

Here's my real problem: In my grid I also have a CheckBox and I want the above style to apply only if the CheckBox is checked.

因此,如果选中了CheckBox,则我希望选择颜色为红色,而如果未选中CheckBox,则希望为蓝色(或默认颜色).

So if the CheckBox is checked i want the selection color to be Red and to be Blue (or default color) if the CheckBox is unchecked.

我浏览了网络,但找不到任何东西,所以我正在寻求帮助.

I went through the web and i can't find anything, so i'm looking for help.

推荐答案

您可以使用两种不同的样式-

You can have two separate styles -

  1. 具有所有设置程序和触发器的默认样式.

  1. Default style with all your setter and triggers.

其中定义了资源的空白样式,并使该样式为 BasedOn 默认样式,以便所有设置器和触发器都从默认样式继承.

Blank style with resources defined in it and make this style to be BasedOn default style so that all setters and triggers gets inherited from default style.

然后您可以根据复选框的选中状态交换 ItemContainerStyle .

And then you can swap ItemContainerStyle based on checkBox checked state.

样品:

<StackPanel>
    <StackPanel.Resources>
        <Style x:Key="myLBStyleDefault">
          <!-- All setters and triggers go here -->
        </Style>
        <Style x:Key="myLBStyleWithRed"
               BasedOn="{StaticResource myLBBaseStyle}"
               TargetType="{x:Type ListBoxItem}">
            <Style.Resources>
                <SolidColorBrush
                    x:Key="{x:Static SystemColors.HighlightBrushKey}" 
                    Color="Red" />
                <SolidColorBrush
                    x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
                    Color="Red" />
            </Style.Resources>
        </Style>
    </StackPanel.Resources>
    <CheckBox x:Name="chk"/>
    <ListBox>
        <ListBox.Style>
            <Style TargetType="ListBox">
                <Setter Property="ItemContainerStyle"
                        Value="{StaticResource myLBStyleDefault}"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsChecked, ElementName=chk}" 
                                 Value="True">
                        <Setter Property="ItemContainerStyle"
                                Value="{StaticResource myLBStyleWithRed}"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ListBox.Style>
    </ListBox>
</StackPanel>

这篇关于更改列表框项目的选择颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆