<ComboBox Height="25" Width="125" Background="Yellow">
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter Property="Background" Value="Yellow"/>
<Setter Property="BorderBrush" Value="Yellow"/>
</Style>
</ComboBox.ItemContainerStyle>
<ComboBoxItem Content="One"/>
<ComboBoxItem Content="Two"/>
<ComboBoxItem Content="Three"/>
</ComboBox>
<ComboBox Height="25" Width="125" Background="Yellow"
Style="{StaticResource {x:Static ToolBar.ComboBoxStyleKey}}">
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter Property="Background" Value="Yellow"/>
<Setter Property="BorderBrush" Value="Yellow"/>
</Style>
</ComboBox.ItemContainerStyle>
<ComboBoxItem Content="One"/>
<ComboBoxItem Content="Two"/>
<ComboBoxItem Content="Three"/>
</ComboBox>
作者对此做出了很好的解释。
for (int s=0; (s < ItemContainerStyle.Setters.Count); s++)
if (((Setter)ItemContainerStyle.Setters[s]).Property == IsEnabledProperty)
{
ItemContainerStyle.Setters.RemoveAt(s);
break;
}
ItemContainerStyle.Setters.Add(new Setter(BackgroundProperty,Brushes.Yellow));
它删除了ISENABLE属性的设置器,这是负责保持Combobox背景灰色的原因。 然后,它添加了一个新的设置器,以使背景为黄色。