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

标签 c# .net wpf xaml label


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

场景

目标

Enable Theming for End User

问题

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

先决条件

Custom Resource Color on all Labels

示例

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

最佳答案

您必须在 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>

并从代码中设置颜色:

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

或者使用十六进制:

this.Resources["CustomLabelColor"] = 
new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF00AAFF"));

关于c# - 编辑 WPF 应用程序中的所有标签颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25158831/

相关文章:

c# - MvcSiteMapProvider 不适用于简单的 MVC.SiteMap 文件

c# - 在按下 Enter 键时绑定(bind) TextBox

c# - Telerik RadWindow

wpf - 为什么不能使用DataTrigger设置TextBox.IsEnabled = True?

c# - 向 TextBlock silverlight 3 添加行为

C# : JSON DeserializeObject giving Exception

c# - UINib 的 Xamarin DequeueReusableCell 始终生成 null

c# - 根据多个因素对列表进行排序

.net - 有人真正使用代码访问安全性来保护他们的程序集和/或方法吗?

c# - 我可以将 FileStream 作为文件的通用接口(interface)返回吗?