2010-06-28 69 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

相關問題