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:
- Set Combobox.Foreground
- Set TextElement.Foreground
- Set TextBlock.Foreground
- Define another implicit TextBlock style in ComboBox.Resources
- Define another implicit TextBlock style in Grid.Resources
- Define another implicit TextBlock style in Window.Resources
Most implicit TextBlock styles will stop at control boundaries, unless you put them in
Application.ResourcesFor example, placing your style in
Window.Resourceswill make it apply to all<TextBlock>objects, but not to text inside other Control Templates such as aComboBoxor aButtonI would suggest moving your style into
Window.Resources, and then styling your ComboBox items to have whatever foreground color you want.If you want to keep it in
Application.Resources, then I suspect you need to track down whatx:Staticbrush key is used for setting theTextBlock.Textcolor and overwrite that in yourComboBox.Resources