带动画的MVVM(我应该使用VisualStateManager吗?)

时间:2010-05-28 11:20:05

标签: wpf animation mvvm datatemplate visualstatemanager

我在Resources-section中有一个带有以下设置的View.xaml:

<DataTemplate DataType="{x:Type ViewModels:MyFirstViewModel}">
    <Views:MyFirstView Content="{Binding}" />
</DataTemplate>

<DataTemplate DataType="{x:Type ViewModels:MySecondViewModel}">
    <Views:MySecondView Content="{Binding}"/>
</DataTemplate>

在View.xaml的内容中我有:

<!-- SelectedMyViewModel is either set to MyFirstViewModel or MySecondViewModel -->
<ContentControl Content="{Binding SelectedMyViewModel}" />

当SelectedMyViewModel发生变化时,我想要一个动画,以便当前视图淡出并且新视图在淡入淡出......

不知怎的,我觉得这应该可以通过VisualStateManager实现 - 但我无法弄清楚如何!

这是一个WPF 4.0项目......

1 个答案:

答案 0 :(得分:0)

您可以在这个问题中找到答案来帮助您WPF Fade Animation

他们正在对可见性进行FadeIn / FadeOut。可以从...中更改触发器

<Trigger Property="Visibility" Value="Visible">

为...

<Trigger Property="Content" Value="{x:Null}">

所以我的想法是,在ViewModel之间进行转换时,你会做类似

的事情
public void SwapViewModels()
{
    // SelectedMyViewModel contains a MyFirstViewModel
    // Setting to null fires the Animation to fadeout
    SelectedMyViewModel = null;

    // Setting the Value to anything but null, should fire the fadein animation
    SelectedMyViewModel = new MySecondViewModel();
}

我没有测试过代码,但希望它能为您提供一个起点。

相关问题