如何在 MVVM 模式中加载 wpf 用户控件 [英] how to load wpf usercontrol in MVVM pattern
问题描述
我正在创建一个 mvvm 模式的 wpf 用户控件.所以我们有:视图(代码隐藏文件中没有代码)、视图模型、模型、数据访问文件.
I'm creating a wpf user control which is in mvvm pattern. So we have : view(with no code in codebehind file), viewmodel,model,dataaccess files.
我有 MainWindow.xaml 作为视图文件,我需要将它与 MainWindowModel.cs 绑定.
I have MainWindow.xaml as a view file, which I need to bind with MainWindowModel.cs.
通常,在 wpf 应用程序中,我们可以使用 App.xaml 文件中的 onStartUp 事件执行此操作.但是在用户控制中,由于我们没有 App.xaml...我该如何实现?
Usually, in a a wpf application we can do this with onStartUp event in App.xaml file. But in user control, as we do not have App.xaml...How do I achieve it ?
请帮忙:(...提前致谢!!!
Please help :(...Thanks in Advance !!!
推荐答案
您可以使用 ContentControl
和 DataTemplate
来绑定 UserControl
> (View) 到 ViewModel :
You can use a ContentControl
, with a DataTemplate
to bind the UserControl
(View) to the ViewModel :
<DataTemplate DataType="{x:Type vm:MyViewModel}">
<v:MyUserControl />
</DataTemplate>
...
<ContentControl Content="{Binding Current}" />
WPF 将根据 Content
这篇关于如何在 MVVM 模式中加载 wpf 用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!