2011-08-18 96 views
0

我有一個組合框,它綁定到一個動畫枚舉。我們有如下要求:如果在組合框中選擇了動畫樣式,則應在文本塊上觸發相應的動畫。我有3個動畫,即淡出,縮放和眨眼。雖然眨眼工作正常,褪色和規模不工作。它僅在第一次加載屏幕時起作用,然後,即使選擇更改爲淡入淡出/縮放,也不會觸發動畫。就像現在一樣,我已經使用數據觸發器觸發了動畫,這就是當這個問題開始時,但是當我們嘗試使用事件觸發器時動畫被完美觸發。我的代碼如下。有人能幫助我解釋我做錯了什麼,以及如何糾正。動畫與數據觸發器無法正常工作

動畫:

 <Storyboard x:Key="Fade"> 
     <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"> 
      <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/> 
     </ObjectAnimationUsingKeyFrames> 
     <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"> 
      <EasingDoubleKeyFrame KeyTime="0" Value="0"/> 
      <EasingDoubleKeyFrame KeyTime="0:0:2" Value="1"/> 
     </DoubleAnimationUsingKeyFrames> 
    </Storyboard> 
    <Storyboard x:Key="Scale"> 
     <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"> 
      <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/> 
     </ObjectAnimationUsingKeyFrames> 
     <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"> 
      <EasingDoubleKeyFrame KeyTime="0" Value="1"/> 
      <EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="1"/> 
     </DoubleAnimationUsingKeyFrames> 
     <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"> 
      <EasingDoubleKeyFrame KeyTime="0" Value="0"/> 
      <EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="1"/> 
     </DoubleAnimationUsingKeyFrames> 
     <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"> 
      <EasingDoubleKeyFrame KeyTime="0" Value="0"/> 
      <EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="1"/> 
     </DoubleAnimationUsingKeyFrames> 
    </Storyboard> 
<Storyboard x:Key="Blink"> 
      <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"> 
       <EasingDoubleKeyFrame KeyTime="0" Value="1"/> 
      </DoubleAnimationUsingKeyFrames> 
      <ObjectAnimationUsingKeyFrames RepeatBehavior="3x" Storyboard.TargetProperty="(UIElement.Visibility)"> 
       <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/> 
       <DiscreteObjectKeyFrame KeyTime="0:0:0.25" Value="{x:Static Visibility.Collapsed}"/> 
       <DiscreteObjectKeyFrame KeyTime="0:0:0.5" Value="{x:Static Visibility.Visible}"/> 
      </ObjectAnimationUsingKeyFrames> 
     </Storyboard> 

組合框,文本塊和數據觸發器用來觸發動畫:

  <telerik:RadComboBox x:Name="animationTypeComboBox" 
           Foreground="White" 
           Width="180" 
           ItemsSource="{Binding Source={StaticResource AnimationTypeEnumValues}}" 
           SelectedItem="{Binding SelectedAnimationType, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" 
           VerticalAlignment="Center" > 
      </telerik:RadComboBox> 
      <TextBlock x:Name="testMsg" Text="Test message" VerticalAlignment="Center" HorizontalAlignment="Center" RenderTransformOrigin="0.5,0.5"> 
       <TextBlock.RenderTransform> 
         <TransformGroup> 
          <ScaleTransform/> 
          <SkewTransform/> 
          <RotateTransform/> 
          <TranslateTransform/> 
         </TransformGroup> 
        </TextBlock.RenderTransform> 
       <TextBlock.Style> 
        <Style TargetType="TextBlock" BasedOn="{StaticResource Subheading}">  
         <Style.Triggers> 
          <DataTrigger Binding="{Binding SelectedAnimationType}" Value="{x:Static ent:AnimationType.Fade}"> 
           <DataTrigger.EnterActions> 
            <BeginStoryboard Storyboard="{StaticResource Fade}"/> 
           </DataTrigger.EnterActions> 
          </DataTrigger> 
          <DataTrigger Binding="{Binding SelectedAnimationType}" Value="{x:Static ent:AnimationType.Scale}"> 
           <DataTrigger.EnterActions> 
            <BeginStoryboard Storyboard="{StaticResource Scale}"/> 
           </DataTrigger.EnterActions> 
          </DataTrigger> 
          <DataTrigger Binding="{Binding SelectedAnimationType}" Value="{x:Static ent:AnimationType.Blink}"> 
           <DataTrigger.EnterActions> 
            <BeginStoryboard Storyboard="{StaticResource Blink}"/> 
           </DataTrigger.EnterActions> 
          </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       </TextBlock.Style> 
      </TextBlock> 

回答

3

我做你的代碼一些改變,使之在我的本地系統工作.. ..這裏是你的問題的解決方案..

<TextBlock x:Name="testMsg" Text="Test message" VerticalAlignment="Center" HorizontalAlignment="Center" RenderTransformOrigin="0.5,0.5"> 
       <TextBlock.RenderTransform> 
         <TransformGroup> 
          <ScaleTransform/> 
          <SkewTransform/> 
          <RotateTransform/> 
          <TranslateTransform/> 
         </TransformGroup> 
        </TextBlock.RenderTransform> 
       <TextBlock.Style> 
        <Style TargetType="TextBlock">  
         <Style.Triggers> 
          <DataTrigger Binding="{Binding SelectedItem,ElementName=AnimStyles}" Value="Fade"> 
           <DataTrigger.EnterActions> 
             <RemoveStoryboard BeginStoryboardName="Scale"/> 
            <RemoveStoryboard BeginStoryboardName="Blink"/> 
            <BeginStoryboard Name="Fade"> 
             <Storyboard> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/> 
              </ObjectAnimationUsingKeyFrames> 
              <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"> 
               <EasingDoubleKeyFrame KeyTime="0" Value="0"/> 
               <EasingDoubleKeyFrame KeyTime="0:0:2" Value="1"/> 
              </DoubleAnimationUsingKeyFrames> 
              </Storyboard> 
            </BeginStoryboard> 
           </DataTrigger.EnterActions> 
          </DataTrigger> 
          <DataTrigger Binding="{Binding SelectedItem,ElementName=AnimStyles}" Value="Scale"> 
           <DataTrigger.EnterActions> 
            <RemoveStoryboard BeginStoryboardName="Fade"/> 
            <RemoveStoryboard BeginStoryboardName="Blink"/> 
            <BeginStoryboard Name="Scale"> 
             <Storyboard> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/> 
              </ObjectAnimationUsingKeyFrames> 
              <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"> 
               <EasingDoubleKeyFrame KeyTime="0" Value="1"/> 
               <EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="1"/> 
              </DoubleAnimationUsingKeyFrames> 
              <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"> 
               <EasingDoubleKeyFrame KeyTime="0" Value="0"/> 
               <EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="1"/> 
              </DoubleAnimationUsingKeyFrames> 
              <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"> 
               <EasingDoubleKeyFrame KeyTime="0" Value="0"/> 
               <EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="1"/> 
              </DoubleAnimationUsingKeyFrames> 
             </Storyboard> 
            </BeginStoryboard> 
           </DataTrigger.EnterActions> 
          </DataTrigger> 
          <DataTrigger Binding="{Binding SelectedItem,ElementName=AnimStyles}" Value="Blink"> 
           <DataTrigger.EnterActions> 
            <RemoveStoryboard BeginStoryboardName="Fade"/> 
            <RemoveStoryboard BeginStoryboardName="Scale"/> 
            <BeginStoryboard Name="Blink"> 
             <Storyboard> 
              <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"> 
               <EasingDoubleKeyFrame KeyTime="0" Value="1"/> 
              </DoubleAnimationUsingKeyFrames> 
              <ObjectAnimationUsingKeyFrames RepeatBehavior="3x" Storyboard.TargetProperty="(UIElement.Visibility)"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/> 
               <DiscreteObjectKeyFrame KeyTime="0:0:0.25" Value="{x:Static Visibility.Collapsed}"/> 
               <DiscreteObjectKeyFrame KeyTime="0:0:0.5" Value="{x:Static Visibility.Visible}"/> 
              </ObjectAnimationUsingKeyFrames> 
             </Storyboard> 
            </BeginStoryboard> 
           </DataTrigger.EnterActions> 
          </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       </TextBlock.Style> 
     </TextBlock> 

在上面的代碼,而不是採取故事板我n我正在採取的資源排隊......並在開始實際故事板之前刪除其他故事板... 這意味着前一個故事板所實施的行爲將被刪除,並且每次動畫的工作方式與新的一樣..

+0

棒極了!完美工作!非常感謝 :) – Sangeetha