2010-06-28 136 views
2

我试图根据设置在xaml中的枚举更改我的标签的颜色。我无法获得更新的颜色。任何帮助都会很棒。WPF更改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> 

代码

public SolidColorBrush BgUnselect { get; set; } 
    public SolidColorBrush FgUnselect { get; set; } 
    public SolidColorBrush BgSelect { get; set; } 
    public SolidColorBrush FgSelect { get; set; } 

    public override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 

     switch (ButtonType) 
     { 
      case ButtonType.Normal: 
       BgUnselect = (SolidColorBrush)FindResource("Normal_bg_Unselect"); 
       FgUnselect = (SolidColorBrush)FindResource("Normal_fg_Unselect"); 
       BgSelect = (SolidColorBrush)FindResource("Normal_bg_Select"); 
       FgSelect = (SolidColorBrush)FindResource("Normal_fg_Select"); 
       return; 

      case ButtonType.OK: 

      case ButtonType.Cancel: 
       return; 
     } 

回答

2

您绑定标签是不完整的,你必须定义的RelativeSource或的ElementName

更改您的用户控件,如下

<UserControl x:Name="userControl" 

,应用在绑定,

Value="{Binding BgSelect, ElementName=userControl}" 

默认绑定验看BgSelect作为用户控件的“DataContext”属性的属性。

此外,由于用户控件自DependencyObject派生,这是不行的,除非你的财产BgSelect等都是依赖属性。

0

背后,在我看来,所有你想要做的是设置前景色和背景属性来定义你的资源。

您是否试过用{StaticResource ...}代替{Binding ...}代码?

例如,更改

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

<Setter Property="Background" Value="{StaticResource Normal_bg_Unselect}" /> 

下面编辑(基于评论)

你当然可以使用样式来控制组的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的更改通知上的颜色画笔。

希望有所帮助。

+0

这工作,但是我想有3套的颜色为一个控制,我可以去按钮类型=正常,并获得normal_bg_unselect为背景。当我将ButtonType设置为Ok时,这将不起作用,因为颜色将设置为正常。 – Ryan 2010-06-28 15:29:51

+0

只是为了澄清,这是一个包含标签的自定义Button控件? – SergioL 2010-06-28 15:54:30

+0

这是正确的 – Ryan 2010-06-28 16:13:55

相关问题