我正在创建一个 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
(视图)到 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