wpf 在 ComboBoxItem 中设置文本颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18478778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:31:17  来源:igfitidea点击:

Setting text color in ComboBoxItem

wpfxamlcolorscombobox

提问by munchine

I want to set the text color for PASSas GREENand the text color for FAILas RED. I can't seem to find the solution. I need to do this in pure XAML.

我想设置PASSasGREEN的文本颜色和FAILas的文本颜色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>

回答by AsitK

You can Use Triggers for the same (you should inherit the base style too)

您可以使用触发器相同(您也应该继承基本样式)

<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>

回答by damien hawks

I would suggest altering your style by creating style document separately in Window.Resources, and then styling your ComboBox items to have whatever foreground color you want.

我建议通过在 Window.Resources 中单独创建样式文档来改变您的样式,然后将您的 ComboBox 项设置为具有您想要的任何前景色。

<ComboBox.Resources>
    <Style TargetType="{x:Type ComboBoxItem}">
        <Setter Property="Foreground" Value="Blue" />
    </Style>
</ComboBox.Resources>

If you want to keep it in Application.Resources, then I think you need to track down what x:Static brush key is used for setting the TextBlock.Text color and overwrite that in your ComboBox.Resources

如果您想将其保留在 Application.Resources 中,那么我认为您需要追踪用于设置 TextBlock.Text 颜色的 x:Static 笔刷键并在您的 ComboBox.Resources 中覆盖它