为什么可以';我是否在WPF中设置所选ListBoxItem的背景色?

为什么可以';我是否在WPF中设置所选ListBoxItem的背景色?,wpf,background,listboxitem,Wpf,Background,Listboxitem,当用户单击ListBoxItem时,我希望它是 大胆的 更大的 红色字体 背景黄 除了背景,一切都正常。 所选项目似乎有一个标准(蓝色)背景。 如何覆盖该选项并将选定的背景更改为黄色 代码如下: <Window x:Class="AlternateListBox2.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microso

当用户单击ListBoxItem时,我希望它是 大胆的 更大的 红色字体 背景黄

除了背景,一切都正常。 所选项目似乎有一个标准(蓝色)背景。 如何覆盖该选项并将选定的背景更改为黄色

代码如下:

<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>

此代码用于设置背景。问题是您需要创建一个ControlTemplate,并将值“Yellow”指定给ContentPresenter的背景属性


这可以做得简单得多。所选列表框项目的背景色取自SystemColor。因此,您需要做的是覆盖列表框资源中的SystemColor:

<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>

谢谢弗朗西斯!!这对我来说确实有点帮助。下面是我的代码,它允许模板对选中和未选中的列表项使用“StrColor”属性

    <Style TargetType="ListBoxItem">
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <!--Nice Brush-->
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <!-- This is a gradient from white to StrColor and back to white -->
                    <!--<GradientStop Color="White" Offset="0"/>
                    <GradientStop Color="{Binding Path=StrColor}" Offset="0.445"/>
                    <GradientStop Color="White" Offset="1"/>
                    <GradientStop Color="{Binding Path=StrColor}" Offset="0.53"/>-->
                    <!-- This is a gradient from StrColor to white -->
                    <GradientStop Color="{Binding Path=StrColor}" Offset="0"/>
                    <GradientStop Color="White" Offset="1"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
        <!--Standard Color-->
        <!--<Setter Property="Background" Value="{Binding Path=StrColor}"/>-->
        <Setter Property="Foreground" Value="{Binding Path=TextColor}"/>
        <Setter Property="Height" Value="{Binding Path=Height}"/>
        <Setter Property="Margin" Value="0"/>
        <Setter Property="Focusable" Value="False"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" x:Name="contentPresenter"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter Property="OpacityMask" TargetName="contentPresenter" Value="{x:Null}"/>
                            <Setter Property="Background" TargetName="Bd">
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="{Binding Path=StrColor}" Offset="0"/>
                                        <GradientStop Color="White" Offset="1"/>
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Foreground" Value="{Binding Path=TextColor}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</UserControl.Resources>


“它可以做得简单得多。选定对象的背景色 列表框项目取自SystemColor。那么,您需要做什么 要做的是覆盖列表框资源中的SystemColor“

覆盖SystemColor(ListBoxItem模板将用于背景/前景)的概念非常糟糕,经常会让WPF新手感到困惑。 因此,我的建议是覆盖ListBoxItem模板并对其进行自定义。

在.net 4.5中不起作用,请参阅
    <Style TargetType="ListBoxItem">
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <!--Nice Brush-->
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <!-- This is a gradient from white to StrColor and back to white -->
                    <!--<GradientStop Color="White" Offset="0"/>
                    <GradientStop Color="{Binding Path=StrColor}" Offset="0.445"/>
                    <GradientStop Color="White" Offset="1"/>
                    <GradientStop Color="{Binding Path=StrColor}" Offset="0.53"/>-->
                    <!-- This is a gradient from StrColor to white -->
                    <GradientStop Color="{Binding Path=StrColor}" Offset="0"/>
                    <GradientStop Color="White" Offset="1"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
        <!--Standard Color-->
        <!--<Setter Property="Background" Value="{Binding Path=StrColor}"/>-->
        <Setter Property="Foreground" Value="{Binding Path=TextColor}"/>
        <Setter Property="Height" Value="{Binding Path=Height}"/>
        <Setter Property="Margin" Value="0"/>
        <Setter Property="Focusable" Value="False"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" x:Name="contentPresenter"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter Property="OpacityMask" TargetName="contentPresenter" Value="{x:Null}"/>
                            <Setter Property="Background" TargetName="Bd">
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="{Binding Path=StrColor}" Offset="0"/>
                                        <GradientStop Color="White" Offset="1"/>
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Foreground" Value="{Binding Path=TextColor}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</UserControl.Resources>