當用戶點擊一個ListBoxItem,我想它是 一個大膽 較大 字體紅色 背景黃色爲什麼我不能在WPF中設置選定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>
無法在.net 4.5中使用,請參閱http://stackoverflow.com/questions/12710296/overriding-listboxitem-background-color-when-not-in-focus-net-4-5/12710338 – 2012-10-03 15:23:24