简体   繁体   English

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

[英]Editing All Label Colors in WPF Applications


I am trying to figure out, how to edit all label colors in my WPF App. 我想弄清楚如何在WPF应用程序中编辑所有标签颜色。
Hope I can get some opinions here. 希望我能在这里得到一些意见

Scenario 脚本

Goal 目标

Enable Theming for End User 为最终用户启用主题

Question

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

Prerequisites 先决条件

Custom Resource Color on all Labels 所有标签上的自定义资源颜色

Example

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

You have to use DynamicResource in XAML: 您必须在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>

And set the color from code: 并通过代码设置颜色:

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

Or to use Hexa: 或使用Hexa:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM