2017-02-26 50 views
1

我有以下buttonlabel內,其foreground color我想綁定到button的:WPF - 元素的顏色勢必按鈕,但按鈕狀態變化不更新

<Button x:Name="login_button" Grid.Row="1" Grid.Column="1" Template="{DynamicResource TileTemplate}" 
     Margin="5,10,5,10"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="2*"/> 
      <RowDefinition Height="4*"/> 
     </Grid.RowDefinitions> 
     <Image Source="pack://application:,,,/textures/logos/lock.png" 
       VerticalAlignment="Stretch" HorizontalAlignment="Left" Grid.Row="0" 
       Margin="10,10,10,10"> 
     </Image> 
     <Label Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Bottom" 
       HorizontalContentAlignment="Left" VerticalContentAlignment="Bottom" 
       Content="LOG IN" FontSize="40" FontFamily="CenturyGothicRegual" 
       Margin="10,10,10,10"> 
      <Label.Foreground> 
       <SolidColorBrush Color="{Binding ElementName=login_button, Path=BorderBrush.Color,UpdateSourceTrigger=PropertyChanged}" 
        Opacity="{Binding ElementName=login_button, Path=BorderBrush.Opacity,UpdateSourceTrigger=PropertyChanged}"/> 
      </Label.Foreground> 
     </Label> 
    </Grid> 
</Button> 

的按鈕被五色使用template

<ControlTemplate x:Key="TileTemplate" TargetType="{x:Type Button}"> 

     <Border x:Name="border" BorderBrush="#99C3C3C3" BorderThickness="2"> 
      <Border.Background> 
       <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
        <GradientStop Color="#28B6B6B6" Offset="0"/> 
        <GradientStop Color="#424D4D4D" Offset="1"/> 
       </LinearGradientBrush> 
      </Border.Background> 
      <VisualStateManager.VisualStateGroups> 
       <VisualStateGroup x:Name="CommonStates"> 
        <VisualState x:Name="Normal"> 
         <Storyboard> 
          <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="border"> 
           <EasingColorKeyFrame KeyTime="0" Value="#42B2B2B2"/> 
          </ColorAnimationUsingKeyFrames> 
          <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="border"> 
           <EasingColorKeyFrame KeyTime="0" Value="#28FFFFFF"/> 
          </ColorAnimationUsingKeyFrames> 
          <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="border"> 
           <EasingColorKeyFrame KeyTime="0" Value="#7F5B5B5B"/> 
          </ColorAnimationUsingKeyFrames> 
         </Storyboard> 
        </VisualState> 
        <VisualState x:Name="MouseOver"> 
         <Storyboard> 
          <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="border"> 
           <EasingColorKeyFrame KeyTime="0" Value="#42B2B2B2"/> 
          </ColorAnimationUsingKeyFrames> 
          <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="border"> 
           <EasingColorKeyFrame KeyTime="0" Value="#28FFFFFF"/> 
          </ColorAnimationUsingKeyFrames> 
          <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="border"> 
           <EasingColorKeyFrame KeyTime="0" Value="#FFD1A139"/> 
          </ColorAnimationUsingKeyFrames> 
          <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Offset)" Storyboard.TargetName="border"> 
           <EasingDoubleKeyFrame KeyTime="0" Value="1"/> 
          </DoubleAnimationUsingKeyFrames> 
          <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Offset)" Storyboard.TargetName="border"> 
           <EasingDoubleKeyFrame KeyTime="0" Value="0"/> 
          </DoubleAnimationUsingKeyFrames> 
          <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="border"> 
           <EasingDoubleKeyFrame KeyTime="0" Value="0.8"/> 
          </DoubleAnimationUsingKeyFrames> 
         </Storyboard> 
        </VisualState> 
        <VisualState x:Name="Pressed"> 
         <Storyboard> 
          <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="border"> 
           <EasingColorKeyFrame KeyTime="0" Value="#FFF0A300"/> 
          </ColorAnimationUsingKeyFrames> 
          <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="border"> 
           <EasingColorKeyFrame KeyTime="0" Value="#5991774A"/> 
          </ColorAnimationUsingKeyFrames> 
          <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="border"> 
           <EasingColorKeyFrame KeyTime="0" Value="#28FFFFFF"/> 
          </ColorAnimationUsingKeyFrames> 
          <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Offset)" Storyboard.TargetName="border"> 
           <EasingDoubleKeyFrame KeyTime="0" Value="0.004"/> 
          </DoubleAnimationUsingKeyFrames> 
          <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="border"> 
           <EasingDoubleKeyFrame KeyTime="0" Value="0.8"/> 
          </DoubleAnimationUsingKeyFrames> 
         </Storyboard> 
        </VisualState> 
        <VisualState x:Name="Disabled"/> 
       </VisualStateGroup> 
      </VisualStateManager.VisualStateGroups> 
      <Grid> 
       <ContentPresenter/> 
      </Grid> 
     </Border> 
    </ControlTemplate> 

我不知道如果模板是很重要的,我只是封閉它情況下,它是。問題是雖然標籤確實在開始時收到按鈕的顏色,但當按鈕的狀態發生更改時(如MouseOverClicked),即使我在綁定中添加了UpdateSourceTrigger=PropertyChanged,標籤的顏色也保持不變。有任何想法嗎?

回答

1

您在ControlTemplate中定義的VisualStates不適用於Button的內容。

您可以定義畫筆或顏色作爲獨立的資源,並在您ControlTemplate應用它們都和一個StyleLabel

<Color x:Key="moColor">#FFD1A139</Color> 
... 
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="border"> 
    <EasingColorKeyFrame KeyTime="0" Value="{StaticResource moColor}"/> 
</ColorAnimationUsingKeyFrames> 

<Button x:Name="login_button" Grid.Row="1" Grid.Column="1" Template="{DynamicResource TileTemplate}" Margin="5,10,5,10"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="2*"/> 
      <RowDefinition Height="4*"/> 
     </Grid.RowDefinitions> 
     <Image Source="pack://application:,,,/textures/logos/lock.png" 
        VerticalAlignment="Stretch" HorizontalAlignment="Left" Grid.Row="0" 
        Margin="10,10,10,10"> 
     </Image> 
     <Label Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Bottom" 
        HorizontalContentAlignment="Left" VerticalContentAlignment="Bottom" 
        Content="LOG IN" FontSize="40" FontFamily="CenturyGothicRegual" 
        Margin="10,10,10,10"> 
      <Label.Style> 
       <Style TargetType="Label"> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource AncestorType=Button}}" Value="True"> 
          <Setter Property="Foreground"> 
           <Setter.Value> 
            <SolidColorBrush Color="{StaticResource moColor}" /> 
           </Setter.Value> 
          </Setter> 
          <Setter Property="Opacity" Value="0.8" /> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </Label.Style> 
     </Label> 
    </Grid> 
</Button> 
+0

我沒有使用它這個乾淨的擔心我搞砸了,所以我改變了一下:D,但現在它工作。感謝您的幫助! – agiro