【问题标题】:Setting AutoCompleteBox ListBox SelectedItem Background Color设置 AutoCompleteBox ListBox SelectedItem 背景颜色
【发布时间】:2018-03-30 10:45:20
【问题描述】:

我正在使用 WPFToolkit 的 AutoCompleteBox,我需要更改所选项目的背景颜色,但我做不到。我可以更改字体样式、字体大小,但不能更改背景。

我尝试了 SO 的许多解决方案,但都没有奏效。到目前为止我尝试过的:

Change background color for selected ListBox item

Changing WPF Listbox SelectedItem text color and highlight/background Color using C#

使用触发器动态更改所选项目的背景

<Style  x:Key="myLBStyle" TargetType="ListBoxItem">
        <Setter Property="Background" Value="IndianRed" />
        <Setter Property="Foreground" Value="WhiteSmoke" />
        <Setter Property="Margin" Value="0" />
        <Setter Property="FontSize" Value="22" />
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="true">
                <Setter Property="Background" Value="Chartreuse" />
                <Setter Property="FontStyle" Value="Italic" />
                <Setter Property="Foreground" Value="Chartreuse" />
            </Trigger>
        </Style.Triggers>
    </Style>

<local:FocusableAutoCompleteBox x:Name="ACBox" Margin="10,32,10,0"
    Grid.Row="2" FontSize="27" Grid.ColumnSpan="4" Foreground="#FF333333"
    Background="#FF1700FF" BorderThickness="2" TextChanged="_ACBox_TextChanged"
    KeyDown="ACBox_KeyDown" Focusable="True" MinimumPopulateDelay="100"
    MinimumPrefixLength="1" ItemContainerStyle="{DynamicResource ResourceKey=myLBStyle}">

我也尝试过覆盖系统颜色:

        <Style  x:Key="myLBStyle" TargetType="ListBoxItem">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Green" />
        </Style.Resources>
        <Setter Property="Background" Value="IndianRed" />
        <Setter Property="Foreground" Value="WhiteSmoke" />
        <Setter Property="Margin" Value="0" />
        <Setter Property="FontSize" Value="22" />
    </Style>

我可以使用触发器成功设置其他属性。我可以设置 selecteditem 的字体为斜体、粗体、更大、更小,但我无法更改所选项目的背景颜色。

【问题讨论】:

    标签: c# wpf listbox


    【解决方案1】:
    <Style  x:Key="myLBStyle" TargetType="ListBoxItem">
        <Setter Property="Background" Value="IndianRed" />
        <Setter Property="Foreground" Value="WhiteSmoke" />
        <Setter Property="Margin" Value="0" />
        <Setter Property="FontSize" Value="22" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Grid Background="{TemplateBinding Background}">
                        <ContentPresenter></ContentPresenter>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="true">
                <Setter Property="Background" Value="Chartreuse" />
                <Setter Property="FontStyle" Value="Italic" />
                <Setter Property="Foreground" Value="Chartreuse" />
            </Trigger>
        </Style.Triggers>
    </Style>
    

    【讨论】:

    • 非常感谢。有效。但是,我想知道为什么它会起作用?我知道我在网格上添加了 ListBox,设置了网格背景。但是为什么我不能改变 ListBox 选择的颜色本身呢?
    • 每一项的颜色在ListBoxItem中定义,不会继承其父控件的颜色。
    • 我明白了。所以这就像控件有一个 ListBoxItem 样式用于整个 ListBox 和另一个 ListBoxItem,它的子项,它包含各个项目。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    相关资源
    最近更新 更多