【发布时间】: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 的字体为斜体、粗体、更大、更小,但我无法更改所选项目的背景颜色。
【问题讨论】: