6

I have a default TextBlock style defined in App.xaml, which seems to also affect the text color of ComboBox items. Now, how do I explicitly set the text color of a ComboBox defined in my main window? (I'd like to keep the default style, but have the combobox text color as, say, blue instead of red...)

App.xaml

<Application x:Class="WpfApplication1.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="Foreground" Value="Red" />
    </Style>
</Application.Resources>

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow">
<Grid>
    <ComboBox Name="comboBox1" SelectedIndex="0" HorizontalAlignment="Left" VerticalAlignment="Top">
        <ComboBoxItem Content = "Item1"/>
        <ComboBoxItem Content = "Item2"/>
        <ComboBoxItem Content = "Item3"/>
    </ComboBox>
</Grid>

Things I've tried:

  1. Set Combobox.Foreground
  2. Set TextElement.Foreground
  3. Set TextBlock.Foreground
  4. Define another implicit TextBlock style in ComboBox.Resources
  5. Define another implicit TextBlock style in Grid.Resources
  6. Define another implicit TextBlock style in Window.Resources

2 Answers 2

5

Most implicit TextBlock styles will stop at control boundaries, unless you put them in Application.Resources

For example, placing your style in Window.Resources will make it apply to all <TextBlock> objects, but not to text inside other Control Templates such as a ComboBox or a Button

I would suggest moving your style into Window.Resources, and then styling your ComboBox items to have whatever foreground color you want.

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

3
  • I tried adding another TextBlock style in the combobox resources. unfortunately, it didn't have any effect. Commented Jan 27, 2012 at 15:08
  • @ErenErsonmez See my edited answer. I didn't notice you had your style in Application.Resources, and not Window.Resources
    – Rachel
    Commented Jan 27, 2012 at 15:24
  • The part about styles not being applied when in Window Resources is only partially true: if in that same Resources there's also a global ComboBox style, the global TextBlock style will apply to the TexBlock in the ComboBox as well. I don't know why it is like that, but the fix is simple enough: within the ComboBox style add <Style.Resources><Style TargetType="{x:Type TextBlock}"></Style></Style.Resources> and it will stop inheriting the globally defined one again.
    – stijn
    Commented Feb 10, 2023 at 8:31
1

You have to use Triggers on the ComboBoxItem

 <Style TargetType="{x:Type ComboBoxItem}">
     <Style.Triggers>
         <Trigger Property="ComboBoxItem.IsMouseOver" Value="true">
             <Setter Property="Foreground" Value="Red"/>
         </Trigger>

         <Trigger Property="ComboBoxItem.IsMouseOver" Value="false">
             <Setter Property="Foreground" Value="Blue"/>
         </Trigger>
     </Style.Triggers>
 </Style>

And if you want to keep it static then

<Style TargetType="{x:Type ComboBoxItem}">
    <Setter Property="Foreground" Value="Blue"/>
</Style>
1
  • Then you'll have to use an x:Key, override the default ComboBox Template, apply the template in a style, and apply the style the the ComboBox
    – MyKuLLSKI
    Commented Jan 27, 2012 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.