Eren Ersönmez
Eren Ersönmez

Reputation: 39085

Override default TextBlock style in ComboBox

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

Upvotes: 6

Views: 4067

Answers (2)

MyKuLLSKI
MyKuLLSKI

Reputation: 5325

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>

Upvotes: 1

Rachel
Rachel

Reputation: 132548

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

Upvotes: 5

Related Questions