为什么我不能设置一个WPF选择一个ListBoxItem的背景颜色? [英] Why can't I set the background color of a selected ListBoxItem in WPF?

查看:192
本文介绍了为什么我不能设置一个WPF选择一个ListBoxItem的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户点击一个ListBoxItem的,我想它是
一个大胆的
较大
红色字体
背景黄

一切正常,除了背景。
它似乎有所选项目标准(蓝色)的背景。
我如何覆盖和更改选定背景黄色的?

下面是code:

 <窗​​口x:类=AlternateListBox2.Window1
    的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
    的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
    标题=窗口1HEIGHT =300WIDTH =300
    XMLNS:地方=CLR的命名空间:AlternateListBox2>
    < Window.Resources>
        <局部:国家X:键=国家/>
        <风格的TargetType ={X:输入一个ListBoxItem}>
            < setter属性=内容VALUE ={绑定路径=名称}/>
            < setter属性=保证金VALUE =2/>
            < Style.Triggers>
                <触发属性=IsSelectedVALUE =真>
                    < setter属性=粗细VALUE =大胆/>
                    < setter属性=字号VALUE =18/>
                    < setter属性=背景值=黄色/>
                    < setter属性=前景VALUE =红/>
                < /触发>
            < /Style.Triggers>        < /样式和GT;
    < /Window.Resources>
    <&StackPanel的GT;
        <列表框
            的ItemsSource ={绑定源= {StaticResource的国家}}
            WIDTH =100
            保证金=10
            的Horizo​​ntalAlignment =左
            />
    < / StackPanel的>
< /窗GT;


解决方案

这是可以做到简单了很多。所选列表框的项目的背景颜色被从将SystemColor服用。所以,你需要做的是覆盖将SystemColor在ListBox的资源:

 < ListBox.Resources>
    <! - 当ListBox中的重点是选择的颜色 - >
    <的SolidColorBrush X:键={X:静态SystemColors.HighlightBrushKey}颜色=黄色/>
    <! - 当列表框不集中选择的颜色 - >
    <的SolidColorBrush X:键={X:静态SystemColors.ControlBrushKey}颜色=黄色/>
< /ListBox.Resources>

When a user clicks on an ListBoxItem, I want to it to be a bold larger font red background yellow

Everything works except the background. It seems that there is a standard (blue) background for the selected item. How do I override that and change the selected background yellow?

Here is the code:

<Window x:Class="AlternateListBox2.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300"
    xmlns:local="clr-namespace:AlternateListBox2">
    <Window.Resources>
        <local:Countries x:Key="countries"/>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Content" Value="{Binding Path=Name}"/>
            <Setter Property="Margin" Value="2"/>
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="FontWeight" Value="Bold"/>
                    <Setter Property="FontSize" Value="18"/>
                    <Setter Property="Background" Value="Yellow"/>
                    <Setter Property="Foreground" Value="Red"/>
                </Trigger>
            </Style.Triggers>

        </Style>
    </Window.Resources>
    <StackPanel>
        <ListBox
            ItemsSource="{Binding Source={StaticResource countries}}"
            Width="100"
            Margin="10"
            HorizontalAlignment="Left"
            />
    </StackPanel>
</Window>

解决方案

It can be done a lot simpler. The Background color for the selected ListBox items are taken from the SystemColors. So, what you need to do is override the SystemColors in the Resources of your ListBox:

<ListBox.Resources>
    <!--Selected color when the ListBox is focused-->
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Yellow" />
    <!--Selected color when the ListBox is not focused-->
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Yellow" />
</ListBox.Resources>

这篇关于为什么我不能设置一个WPF选择一个ListBoxItem的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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