更改 ContentTemplate 时的动画
我有一个窗口,随着时间的推移必须显示不同的控件。我搜索了使用 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>
到目前为止工作正常,但我很好奇两件事:
- 是否有更好的 mvvm 方法?
- 我怎样才能为即将显示的新数据模板中的项目执行动画?
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:
- is there a better approach with mvvm?
- how can i execute an animation for the items in the new datatemplate that is about to be shown?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于问题#2:
您可以在模板内的控件中使用 EventTrigger 来启动动画,如下所示:
For the question #2:
You could use EventTrigger in controls within you templates to start animation like it is done below:
由于动画是特定于视图的操作,因此它们应该从视图背后的代码运行,而不是从视图模型运行。在过去,我已经连接到一个事件,然后从代码隐藏中运行以下命令:
至于问题#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:
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.