0

我在资源部分有一个 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 项目...

4

1 回答 1

0

您也许可以在这个问题中为您提供答案,以帮助您WPF Fade Animation

他们正在对可见性进行淡入/淡出。可能可以将触发器从...更改为

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

至...

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

所以我的想法是在 ViewModels 之间转换时你会做类似的事情

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();
}

我还没有测试过代码,但希望它能给你一个起点。

于 2010-05-29T02:03:28.823 回答