2011-09-23 77 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中的一個屬性。