我正在使用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 的字体设置为斜体,粗体,更大,更小,但我无法更改所选项目的背景颜色。
答案 0 :(得分:0)
<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>