【问题标题】:Setting text color in ComboBoxItem在 ComboBoxItem 中设置文本颜色
【发布时间】:2013-08-28 03:11:15
【问题描述】:
我想将 PASS 的文本颜色设置为 GREEN,将 FAIL 的文本颜色设置为 RED。我似乎找不到解决方案。我需要在纯 XAML 中执行此操作。
<ComboBox x:Name="LocatedCorrectly" Width="100"
Height="25" Grid.Column="1" Grid.Row="2"
HorizontalAlignment="Left"
IsSynchronizedWithCurrentItem="True">
<ComboBoxItem Content="PASS" Tag="PASS" IsSelected="True"/>
<ComboBoxItem Content="FAIL" Tag="FAILED" />
</ComboBox>
【问题讨论】:
标签:
wpf
xaml
colors
combobox
【解决方案1】:
您也可以使用触发器(您也应该继承基本样式)
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Foreground" Value="Blue" />
<Style.Triggers>
<Trigger Property="Content" Value="PASS">
<Setter Property="Foreground" Value="Green"/>
</Trigger>
</Style.Triggers>
</Style>
【解决方案2】:
我建议通过在 Window.Resources 中单独创建样式文档来更改您的样式,然后将您的 ComboBox 项目设置为具有您想要的任何前景色。
<ComboBox.Resources>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Foreground" Value="Blue" />
</Style>
</ComboBox.Resources>
如果您想将其保留在 Application.Resources 中,那么我认为您需要追踪用于设置 TextBlock.Text 颜色的 x:Static 画笔键并将其覆盖在您的 ComboBox.Resources 中