2011-09-23 81 views
0

我有一个窗口,其中必须随时间显示不同的控件。我搜索了使用MVVM模式的解决方案,并结束了与此更改ContentTemplate时的动画

<ContentControl Content="{Binding}"> 
     <ContentControl.Style> 
      <Style TargetType="ContentControl"> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding ViewType}" Value="RecipeList"> 
         <Setter Property="ContentTemplate" Value="{StaticResource RecipeTemplate}"/> 
        </DataTrigger> 
        <DataTrigger Binding="{Binding ViewType}" Value="Default"> 
         <Setter Property="ContentTemplate" Value="{StaticResource DefaultTemplate}"/> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </ContentControl.Style> 
</ContentControl> 

这工作正常,到目前为止,但我很好奇的两件事情:

  1. 有与MVVM更好的方法?
  2. 我该如何执行即将显示的新数据模式中的项目的动画?

回答

1

对于问题2:

你可以在控制之内,你的模板使用EventTrigger来开始动画像它下面做:

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Grid.Triggers> 
      <EventTrigger RoutedEvent="FrameworkElement.Loaded"> 
       <BeginStoryboard> 
        <Storyboard x:Name="SomeStoryBoard"/> 
       </BeginStoryboard> 
      </EventTrigger> 
     </Grid.Triggers> 
    </Grid> 
</Window> 
+0

我觉得这是一个很好的做法是有一个事件, ContentTemplate改变时触发? – StefanG

1

由于动画是特定于视图的操作,因此它们应该从视图后面的代码运行,而不是ViewModel。在过去,我已经勾搭成一个事件,只是运行在后台代码如下:

Storyboard animation = (Storyboard)panel.FindResource("MyAnimation"); 
animation.Begin(); 

至于问题#1,我没有看到你的代码中的任何问题,以显示不同的视图基于ViewModel中的一个属性。