编辑WPF应用程序中的所有标签颜色

本文关键字:标签 颜色 WPF 应用程序 编辑 | 更新日期: 2023-09-27 18:16:56


我正在努力弄清楚,如何在我的WPF应用程序中编辑所有标签颜色。
希望我能得到一些意见在这里。

为最终用户启用主题

How to edit alle labels at once, Where the Color is bound to the Custom Color Resource

先决条件

自定义所有标签的资源颜色

<Label.Foreground>
                <SolidColorBrush Color="{DynamicResource CustomLabelColor}"/>
</Label.Foreground>

编辑WPF应用程序中的所有标签颜色

你必须在XAML中使用DynamicResource:

<Window.Resources>
   <SolidColorBrush x:Key="CustomLabelColor" />
    <Style TargetType="Label">
            <Setter Property="Foreground" Value="{DynamicResource CustomLabelColor}" />
    </Style>
    </Window.Resources>
    <StackPanel>
        <Label Content="fsdaf"></Label>
        <Label Content="fsdaf"></Label>
        <Label Content="fsdaf"></Label>
        <Label Content="fsdaf"></Label>
        <Label Content="fsdaf"></Label>
</StackPanel>

并设置color from code:

this.Resources["CustomLabelColor"] = new SolidColorBrush(Colors.Aqua);

或者使用Hexa:

this.Resources["CustomLabelColor"] = 
new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF00AAFF"));
<Style TargetType="Label">
        <Setter Property="Foreground" Value="{StaticResource CustomLabelColor}" />
   </Style>