更改 ContentTemplate 时的动画

发布于 2024-12-06 13:30:30 字数 992 浏览 0 评论 0原文

我有一个窗口,随着时间的推移必须显示不同的控件。我搜索了使用 mvvm 模式的解决方案,最终得到了这个

<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. 我怎样才能为即将显示的新数据模板中的项目执行动画?

I have a window where different controls had to be displayed over time. I searched for a solution with using the mvvm pattern and ended up with this

<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>

This works fine so far but i'm curious about two things:

  1. is there a better approach with mvvm?
  2. how can i execute an animation for the items in the new datatemplate that is about to be shown?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

会发光的星星闪亮亮i 2024-12-13 13:30:30

对于问题#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>

For the question #2:

You could use EventTrigger in controls within you templates to start animation like it is done below:

<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>
梦情居士 2024-12-13 13:30:30

由于动画是特定于视图的操作,因此它们应该从视图背后的代码运行,而不是从视图模型运行。在过去,我已经连接到一个事件,然后从代码隐藏中运行以下命令:

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

至于问题#1,我没有发现您的代码根据 ViewModel 中的属性显示不同的视图有任何问题。

Since Animations are View-Specific actions, they should be run from the Code-Behind the View, not the ViewModel. In the past, I've hooked into an Event and just run the following from the code-behind:

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

As for question #1, I don't see any problem with your code for displaying a different View based on a property in the ViewModel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文