代码之家  ›  专栏  ›  技术社区  ›  John Baird

正在尝试从resourcedictionary设置前景文本颜色

wpf
  •  0
  • John Baird  · 技术社区  · 5 年前

    我正在尝试创建一个标签样式,该样式应用在笔刷资源目录中定义的前景色。

    <Color x:Key="TextForegroundColor" >#8B4513</Color>
    
    <SolidColorBrush 
        x:Key="TextForegroundColorBrush"
        Color="{Binding Source={StaticResource TextForegroundColor}, Path=Color}" />
    
    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
    <Style x:Key="MVILabelStyle" TargetType="{x:Type Label}">
        <Setter Property="HorizontalAlignment" Value="Right" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="Foreground" Value="{StaticResource TextForegroundColorBrush}" />
    </Style>
    

    enter image description here

    1 回复  |  直到 5 年前
        1
  •  0
  •   Clemens    5 年前

    装订

    Color="{Binding Source={StaticResource TextForegroundColor}, Path=Color}"
    

    这是错误的。它要求颜色具有颜色属性。

    你要么写

    <SolidColorBrush Color="{Binding Source={StaticResource TextForegroundColor}}"/>
    

    <SolidColorBrush Color="{StaticResource TextForegroundColor}"/>
    
        2
  •  0
  •   John Baird    5 年前

    好的,经过三天的搜索和删除,我终于找到了一个应用了样式的控件和一个没有应用样式的控件。在一步一步地比较每个东西的过程中,我在codebhind中找到了一个语句,该语句禁用了控件。事情就是这样。它被禁用,因此显示出正确的风格。我启用了控件,并应用了bingo样式。感谢所有参与的人。