[WPF覆盖TextBlock的全局样式

问题描述 投票:0回答:1

我的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>

但是它不起作用:

enter image description here

编辑1:我正在使用WPF .NET Core 3.0

c# .net wpf xaml .net-core
1个回答
0
投票

问题出在部分:

    <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>
© www.soinside.com 2019 - 2024. All rights reserved.