我在 App.xaml 中定义了默认的 TextBlock 样式,它似乎也会影响 ComboBox 项目的文本颜色。现在,如何显式设置主窗口中定义的 ComboBox 的文本颜色? (我想保留默认样式,但将组合框文本颜色设置为蓝色而不是红色......)
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>
主窗口.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>
我尝试过的事情:
- 设置 Combobox.Foreground
- 设置 TextElement.Foreground
- 设置 TextBlock.Foreground
- 在 ComboBox.Resources 中定义另一个隐式 TextBlock 样式
- 在 Grid.Resources 中定义另一个隐式 TextBlock 样式
- 在 Window.Resources 中定义另一个隐式 TextBlock 样式
大多数隐式 TextBlock 样式将停止在控件边界处,除非您将它们放入Application.Resources
例如,将您的样式放入Window.Resources
将使之适用于所有人<TextBlock>
对象,但不包括其他控件模板内的文本,例如ComboBox
or a Button
我建议将你的风格转移到Window.Resources
,然后将您的 ComboBox 项目设计为具有您想要的任何前景色。
<ComboBox.Resources>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Foreground" Value="Blue" />
</Style>
</ComboBox.Resources>
如果你想保留它Application.Resources
,那么我怀疑你需要追踪什么x:Static
画笔键用于设置TextBlock.Text
颜色并覆盖你的ComboBox.Resources
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)