2010-08-17 5 views
0

버튼을 다시 작성하고 controlTemplate을 제공하고 일부 트리거 (mouseenter 및 mouseleave)를 추가합니다.예외 : "Storyboard '객체의 키 값이 누락되었습니다. 스타일이 지정된 트리거를 사용합니다.

<Style TargetType="Button"> 
    <Style.Resources> 
     <Color x:Key="ForeColour" R="128" G="128" B="128" A="255"/> 

     <Color x:Key="BackColour" R="211" G="211" B="211" A="255"/> 

     <Color x:Key="GlowColour" R="30" G="144" B="255" A="255"/> 

     <SolidColorBrush Color="{StaticResource ResourceKey=ForeColour}" x:Key="ForeBrush"/> 

     <LinearGradientBrush x:Key="BackBrush" StartPoint="0,1" EndPoint="0,0"> 
      <GradientStop Color="White" Offset="0.5"/> 
      <GradientStop Color="{StaticResource ResourceKey=BackColour}" Offset="1"/> 
     </LinearGradientBrush> 

     <Storyboard x:Name="mouseOverText"> 
      <ColorAnimation Storyboard.Target="{Binding RelativeSource= {RelativeSource Self}" Storyboard.TargetProperty="Foreground" From="{StaticResource ResourceKey=ForeColour}" To="{StaticResource ResourceKey=GlowColour}"/> 
     </Storyboard> 

    </Style.Resources> 

    <Setter Property="Foreground" Value="{StaticResource ResourceKey=ForeBrush}"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
       <Border BorderBrush="LightGray" BorderThickness="2" CornerRadius="8" Background="{StaticResource ResourceKey=BackBrush}"> 
         <ContentPresenter x:Name="cnpContent" Opacity="1" VerticalAlignment="Center" HorizontalAlignment="Center" Content="{TemplateBinding Content}"/> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <EventTrigger RoutedEvent="MouseEnter"> 
      <BeginStoryboard Storyboard="{StaticResource mouseOverText}"/> 
     </EventTrigger> 
    </Style.Triggers> 
</Style> 

을하지만 난 그것을 실행할 때 내가 시작시 다음과 같은 오류가 발생합니다 : 다음과 같이

내 XAML은

<ColorAnimation Storyboard.Target="{Binding RelativeSource= {RelativeSource Self}" Storyboard.TargetProperty="Foreground" From="{StaticResource ResourceKey=ForeColour}" To="{StaticResource ResourceKey=GlowColour}"/> 

:이 라인에 해당

'Missing key value on 'Storyboard' object.' Line number '88' and line position '31'. 

내가 마우스를 가리키면 단추의 전경이 다른 색으로 희미 해지는 것입니다. 아무도 내가 잘못 갈 생각이있어? 나는 그것을 짐작할 수 없다.

답변

3

세 가지 문제점이 있습니다.

하나는 키를 지정하지 않고 스토리 보드에 스토리 보드를 추가하는 것입니다. 나는 x:Name 대신에 x:Key을 원한다고 생각합니다.

다음으로 Storyboard.TargetRelativeSource Self으로 바인딩으로 설정하면 목표를 ColorAnimation 자체로 설정하게됩니다. 실제로는 그 속성을 그대로 둘 수 있으며 기본적으로 Button을 대상으로합니다.

마지막으로 TargetProperty가 Foreground으로 설정되었지만 실제로는 전경 브러시의 Color 속성에 애니메이션을 적용하려고하므로 Foreground.Color을 사용해야합니다. 사용해보기 :

<Storyboard x:Key="mouseOverText"> 
    <ColorAnimation 
     Storyboard.TargetProperty="Foreground.Color" 
     From="{StaticResource ResourceKey=ForeColour}" 
     To="{StaticResource ResourceKey=GlowColour}"/> 
</Storyboard> 
+0

대단히 감사합니다. –

관련 문제