我正在使用MVVM为全屏某种Kiosk开发我的第一个WPF应用程序。
我需要更改上下文(视频 View ,文本 View ,PowerPoint View )以响应异步事件。
我遇到了麻烦,因为我已经在MVVM中定义了以下数据上下文,但尚未能够在它们之间进行切换:
<Window.Resources>
<DataTemplate DataType="{x:Type vm:VideoViewModel}">
<v:VideoView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:PowerpointViewModel}">
<v:PowerpointView />
</DataTemplate>
</Window.Resources>
任何帮助将不胜感激,谢谢。
最佳答案
通常,我有一个ShellViewModel
,其中包含一个CurrentPage
属性,该属性包含当前页面的ViewModel。在XAML中,我将ContentControl.Content
绑定(bind)到CurrentPage
,然后切换 View ,只需将CurrentPage属性切换到任何应为当前的ViewModel即可。
<ContentControl Content="{Binding CurrentPage}" />
更改页面命令:
void ChangePage(ViewModelBase page)
{
CurrentPage = page;
}
例如,请参阅我的this post。
关于wpf - 如何在WPF中更改上下文,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8309689/