0
我正在使用WPFToolkit的AutoCompleteBox,我需要更改选定的项目背景颜色,但我做不到。我可以更改字体样式,字体大小,而不是背景。设置AutoCompleteBox列表框SelectedItem背景颜色
我已经尝试了很多从SO的解决方案,但是他们都没有工作。我试过到目前为止:
Change background color for selected ListBox item
Changing WPF Listbox SelectedItem text color and highlight/background Color using C#
使用触发器来更改selectedItem属性的背景动态
<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的字体为斜体,粗体,较大,较小,但我无法更改所选项目的背景颜色。
非常感谢。有效。但是,我想知道_why_是否有效?我知道我在网格上添加了ListBox,并设置了网格背景。但为什么我不能更改ListBox选择的颜色本身? –
每个项目的颜色在'ListBoxItem'中定义,它不会继承其父控件的颜色。 – Iron
我明白了。所以它就像控件有一个ListBoxItem风格的整体ListBox和另一个ListBoxItem,它的子,它拥有单独的项目。 –