2017-02-26 51 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