2013-07-26 25 views
2

我有这样的视图模型:动画,Image.Source和绑定

public class ViewModel : ViewModelBase 
{ 
    public ViewModel() 
    { 
     // indeed, images are dynamically generated 
     normal = new BitmapImage(new Uri("Normal.jpg", UriKind.Relative)); 
     flash = new BitmapImage(new Uri("Flash.jpg", UriKind.Relative)); 
    } 

    public bool IsFlashing 
    { 
     get { return isFlashing; } 
     set 
     { 
      if (isFlashing != value) 
      { 
       isFlashing = value; 
       OnPropertyChanged("IsFlashing"); 
      } 
     } 
    } 
    private bool isFlashing; 

    public ImageSource Normal 
    { 
     get { return normal; } 
     private set 
     { 
      if (normal != value) 
      { 
       normal = value; 
       OnPropertyChanged("Normal"); 
      } 
     } 
    } 
    private ImageSource normal; 

    public ImageSource Flash 
    { 
     get { return flash; } 
     set 
     { 
      if (flash != value) 
      { 
       flash = value; 
       OnPropertyChanged("Flash"); 
      } 
     } 
    } 
    private ImageSource flash; 
} 

我想在视图中(从NormalFlash,然后再返回)动画图像,IsFlashing == true时。 为了证明,我想要实现,我会发布了一块XAML的:

 <DataTemplate x:Key="MyTemplate"> 
      <Grid> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="Auto"/> 
        <RowDefinition /> 
       </Grid.RowDefinitions> 

       <CheckBox Content="Is flashing" IsChecked="{Binding IsFlashing}"/> 

       <Image x:Name="MyImage" Grid.Row="1" Source="{Binding Normal}"/> 
      </Grid> 

      <DataTemplate.Triggers> 
       <DataTrigger Binding="{Binding IsFlashing}" Value="True"> 
        <DataTrigger.EnterActions> 
         <BeginStoryboard> 
          <Storyboard> 
           <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MyImage" 
                   Storyboard.TargetProperty="Source" 
                   RepeatBehavior="Forever"> 
            <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{Binding Normal}"/> 
            <DiscreteObjectKeyFrame KeyTime="0:0:1" Value="{Binding Flash}"/> 
           </ObjectAnimationUsingKeyFrames> 
          </Storyboard> 
         </BeginStoryboard> 
        </DataTrigger.EnterActions> 
       </DataTrigger> 
      </DataTemplate.Triggers> 
     </DataTemplate> 

这是不行的,因为动画引擎无法绑定冻结故事板。 有没有其他办法可以达到这个目的?

我可以解决这个在视图模型中使用计时器...但这种方法闻起来。

UPDATE 根据Richard Deeming的回答,数据模板是这样看的。它的工作原理,它肯定更好,比定时器:

 <DataTemplate x:Key="MyTemplate"> 
      <Grid> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="Auto"/> 
        <RowDefinition /> 
       </Grid.RowDefinitions> 

       <CheckBox Content="Is flashing" IsChecked="{Binding IsFlashing}"/> 

       <Image x:Name="NormalImage" Grid.Row="1" Source="{Binding Normal}"/> 
       <Image x:Name="FlashImage" Grid.Row="1" Source="{Binding Flash}" Visibility="Collapsed"/> 
      </Grid> 

      <DataTemplate.Triggers> 
       <DataTrigger Binding="{Binding IsFlashing}" Value="True"> 
        <DataTrigger.EnterActions> 
         <BeginStoryboard> 
          <Storyboard> 
           <ObjectAnimationUsingKeyFrames Storyboard.TargetName="NormalImage" 
                   Storyboard.TargetProperty="Visibility" 
                   RepeatBehavior="Forever" 
                   Duration="0:0:1"> 
            <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Visible}"/> 
            <DiscreteObjectKeyFrame KeyTime="0:0:0.5" Value="{x:Static Visibility.Collapsed}"/> 
           </ObjectAnimationUsingKeyFrames> 
          </Storyboard> 
         </BeginStoryboard> 
         <BeginStoryboard> 
          <Storyboard> 
           <ObjectAnimationUsingKeyFrames Storyboard.TargetName="FlashImage" 
                   Storyboard.TargetProperty="Visibility" 
                   RepeatBehavior="Forever" 
                   Duration="0:0:1"> 
            <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Collapsed}"/> 
            <DiscreteObjectKeyFrame KeyTime="0:0:0.5" Value="{x:Static Visibility.Visible}"/> 
           </ObjectAnimationUsingKeyFrames> 
          </Storyboard> 
         </BeginStoryboard> 
        </DataTrigger.EnterActions> 
        <DataTrigger.ExitActions> 
         <BeginStoryboard> 
          <Storyboard FillBehavior="Stop"> 
           <ObjectAnimationUsingKeyFrames Storyboard.TargetName="NormalImage" 
                   Storyboard.TargetProperty="Visibility" 
                   Duration="0:0:1"> 
            <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Visible}"/> 
           </ObjectAnimationUsingKeyFrames> 
          </Storyboard> 
         </BeginStoryboard> 
         <BeginStoryboard> 
          <Storyboard FillBehavior="Stop"> 
           <ObjectAnimationUsingKeyFrames Storyboard.TargetName="FlashImage" 
                   Storyboard.TargetProperty="Visibility" 
                   Duration="0:0:1"> 
            <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Collapsed}"/> 
           </ObjectAnimationUsingKeyFrames> 
          </Storyboard> 
         </BeginStoryboard> 
        </DataTrigger.ExitActions> 
       </DataTrigger> 
      </DataTemplate.Triggers> 
     </DataTemplate> 

回答

2

最简单的解决方案可能是有两个重叠的图像 - 一个绑定到Normal源,和一个绑定到Flash源。然后您可以使用Storyboard在两幅图像上为Visibility属性设置动画。

+0

好主意。我会试试看。 – Dennis