【发布时间】:2017-06-02 02:38:48
【问题描述】:
我有一个在 TargetType ToggleButton 上设置了样式的组合框
<ComboBox x:Name="comboBox1" Style="{StaticResource ComboBoxBlue}" HorizontalAlignment="Left" Margin="10,128,0,0" VerticalAlignment="Top" Width="75" />
项目是使用 C# 使用列表项目源动态设置的
public static List<string> MyItemSource = new List<string>()
{
"Item 1", "Item 2", "Item 3", "Item 4"
};
comboBox1.ItemsSource = MyItemSource;
设置项目背景颜色(全局)
<!-- ComboBox Blue Item -->
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="Blue" />
<Setter Property="BorderBrush" Value="Blue" />
</Style>
但是如何设置 x:Key 使其仅适用于某些组合框?
<Style x:Key="ComboBoxBlueItem" TargetType="{x:Type ComboBoxItem}">
我可以在每个 ComboBox 上使用 ComboBox.ItemContainerStyle 标签,但是我必须单独设置每个 ComboBox 的样式。
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Background" Value="Blue" />
</Style>
</ComboBox.ItemContainerStyle>
【问题讨论】: