为什么我不能在WPF中设置所选ListBoxItem的背景颜色?

时间:2009-01-29 11:39:15

标签: wpf background listboxitem

当用户点击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>

4 个答案:

答案 0 :(得分:12)

可以做得更简单。所选ListBox项的背景颜色取自SystemColors。因此,您需要做的是覆盖ListBox的资源中的SystemColors:

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

答案 1 :(得分:3)

此代码适用于设置背景。问题是你需要创建一个ControlTemplate并为ContentPresenter的Background属性赋值“Yellow”。

    <Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}">
        <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" Value="Yellow"/>
                            <Setter Property="FontWeight" Value="Bold"/>
                            <Setter Property="FontSize" Value="18"/>
                            <Setter Property="Foreground" Value="Red"/>
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="true"/>
                                <Condition Property="Selector.IsSelectionActive" Value="false"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                        </MultiTrigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

答案 2 :(得分:2)

  

“它可以做得更简单。所选的背景颜色   ListBox项目取自SystemColors。那么,你需要什么   do覆盖ListBox资源中的SystemColors“

重写SystemColors的概念,ListBoxItem模板将用于后台/前台是非常糟糕的,并且经常会混淆WPF新手的人。 因此,我的建议是覆盖ListBoxItem模板并自定义它。

答案 3 :(得分:0)

谢谢弗朗西丝!这对我来说很有意义。这是我的代码,允许模板对选定和未选择的列表项使用“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>