我的App.xaml:
<Application x:Class="Tracker.WPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Tracker.WPF.Views"
xmlns:vm="clr-namespace:Tracker.WPF.ViewModels"
Exit="Application_Exit">
<Application.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value ="#aaaaaa"/>
</Style>
</Application.Resources>
</Application>
为所有TextBlocks定义浅灰色,因为我的Windows背景是深色的。一切都很好。
我有一个ComboBox,背景为白色,很好。我想使用黑色文本,但改用浅灰色前景色。
我试图以此方式覆盖它:
<ComboBox SelectedValue="{Binding ForceCardPopupSide}" SelectedValuePath="Key" DisplayMemberPath="Value"
ItemsSource="{Binding ForceCardPopupSides, Mode=OneTime}">
<ComboBox.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Black" />
</Style>
</ComboBox.Resources>
</ComboBox>
但是它不起作用:
问题出在部分:
<ComboBox.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Black" />
</Style>
</ComboBox.Resources>
[您知道,ComboBox
不是TextBlock
,也不是从中衍生出来的。因此不应用此样式。
解决方法是改为设置TextBlock.Foreground
:
<ComboBox.Resources>
<Style TargetType="ComboBox">
<Setter Property="TextBlock.Foreground" Value="Black" />
</Style>
</ComboBox.Resources>