WPF基于非代码隐藏属性更改Xaml中的颜色

WPF基于非代码隐藏属性更改Xaml中的颜色,wpf,xaml,binding,Wpf,Xaml,Binding,我正在尝试根据xaml中的枚举集更改标签的颜色。我无法更新颜色。任何帮助都会很好 谢谢 <UserControl.Resources> <!-- Normal --> <SolidColorBrush x:Key="Normal_bg_Unselect" Color="#FF1A73CC" /> <SolidColorBrush x:Key="Normal_fg_Unselect" Color="#FF72BAFF" />

我正在尝试根据xaml中的枚举集更改标签的颜色。我无法更新颜色。任何帮助都会很好

谢谢

<UserControl.Resources>
    <!-- Normal -->
    <SolidColorBrush x:Key="Normal_bg_Unselect" Color="#FF1A73CC" />
    <SolidColorBrush x:Key="Normal_fg_Unselect" Color="#FF72BAFF" />
    <SolidColorBrush x:Key="Normal_bg_Select" Color="#FF1ACCBF" />
    <SolidColorBrush x:Key="Normal_fg_Select" Color="#FF91FFFF" />


</UserControl.Resources>


<Grid>
    <Label Name="BackgroundLabel" Width="Auto" Height="Auto" BorderThickness="0" Panel.ZIndex="1" Cursor="Hand">
        <Label.Foreground>
            <SolidColorBrush Color="{DynamicResource Color_LightBlue}"/>
        </Label.Foreground>
        <Label.Style>
            <Style TargetType="{x:Type Label}">
                <Setter Property="Background" Value="{Binding BgUnselect}" />
                <Setter Property="Foreground" Value="{Binding FgUnselect}" />
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="{Binding BgSelect}" />
                        <Setter Property="Foreground" Value="{Binding FgSelect}" />
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="False">
                        <Setter Property="Background" Value="{Binding BgUnselect}" />
                        <Setter Property="Foreground" Value="{Binding FgUnselect}" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Label.Style>
        <Label.OpacityMask>
            <LinearGradientBrush>
                <GradientStop Color="#00FFFFFF" Offset="-.35"/>
                <GradientStop Color="#FFFFFFFF" Offset="1"/>
            </LinearGradientBrush>
        </Label.OpacityMask>
    </Label>
    <TextBlock Name="ContentLabel" Text="{Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, FallbackValue='Styled Button'}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="20,0,0,0" FontFamily="/HarringtonGroup.TrainingBuilder;component/Fonts/#HelveticaNeue" FontSize="30" Foreground="{Binding ElementName=BackgroundLabel, Path=Foreground}" />
</Grid>

在我看来,您所要做的只是将前台和后台属性设置为您定义的资源

您是否尝试过将
{Binding…}
代码替换为
{StaticResource…}

例如,改变

<Setter Property="Background" Value="{Binding BgUnselect}" /> 


编辑以下内容(根据评论)

可以想象,您可以使用样式来控制每种按钮类型的4种颜色。我创建了一个可以应用于代码的小示例。如果不清楚,我将尝试重写您的代码示例

创建基础样式:

<Style x:Key="LabelStyleBase" TargetType="{x:Type Label}">
    <Setter Property="Foreground" Value="{DynamicResource ForegroundBrush}"/>
    <Setter Property="Background" Value="{DynamicResource BackgroundBrush}"/>
        <!-- more style settings -->
</Style>

然后创建您的变体:

<Style x:Key="LabelStyle1" BasedOn="{StaticResource LabelStyleBase}" TargetType="{x:Type Label}">
    <Style.Resources>
        <SolidColorBrush x:Key="ForegroundBrush" Color="Purple" />
        <SolidColorBrush x:Key="BackgroundBrush" Color="Pink" />    
    </Style.Resources>
</Style> 

<Style x:Key="LabelStyle2" BasedOn="{StaticResource LabelStyleBase}" TargetType="{x:Type Label}">
    <Style.Resources>
        <SolidColorBrush x:Key="ForegroundBrush" Color="Aqua" />
        <SolidColorBrush x:Key="BackgroundBrush" Color="Yellow" />  
    </Style.Resources>
</Style>

您可能会得到一个资源找不到的警告,但这应该没问题

替代解决方案

最后,如果您不想这样做,您可能必须在类上实现INotifyPropertyChanged,并重写笔刷属性上的设置器以触发NotifyPropertyChanged事件

有点不清楚您是如何实现自定义按钮控件的,但是您可能应该将按钮类型枚举作为DependencyProperty公开,并在DependencyProperty的更改通知上更改颜色笔刷


希望对您有所帮助。

您的绑定标记不完整,必须定义RelativeSource或ElementName

如下所示更改您的UserControl

<UserControl x:Name="userControl"
默认情况下,绑定将BgSelect作为用户控件的“DataContext”属性的属性查找


另外,由于UserControl是从DependencyObject派生的,除非您的属性BgSelect等是dependency属性,否则这将不起作用。

这是可行的,但是我希望一个控件有3组颜色,其中我可以转到ButtonType=Normal并将Normal\u bg\u unselect作为背景。当我将ButtonType设置为Ok时,这将不起作用,因为颜色将设置为normal。只是为了澄清一下,这是一个包含标签的自定义按钮控件?
<Style x:Key="LabelStyle1" BasedOn="{StaticResource LabelStyleBase}" TargetType="{x:Type Label}">
    <Style.Resources>
        <SolidColorBrush x:Key="ForegroundBrush" Color="Purple" />
        <SolidColorBrush x:Key="BackgroundBrush" Color="Pink" />    
    </Style.Resources>
</Style> 

<Style x:Key="LabelStyle2" BasedOn="{StaticResource LabelStyleBase}" TargetType="{x:Type Label}">
    <Style.Resources>
        <SolidColorBrush x:Key="ForegroundBrush" Color="Aqua" />
        <SolidColorBrush x:Key="BackgroundBrush" Color="Yellow" />  
    </Style.Resources>
</Style>
<UserControl x:Name="userControl"
Value="{Binding BgSelect, ElementName=userControl}"