【问题标题】:How to apply style to dynamic ComboBox Items?如何将样式应用于动态组合框项目?
【发布时间】: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>

【问题讨论】:

    标签: c# wpf xaml combobox


    【解决方案1】:

    这将对该组合框中的每个项目使用上述样式:

    <ComboBox ItemContainerStyle="{StaticResource ComboBoxBlueItem}" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-19
      • 1970-01-01
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多