简体   繁体   English

WPF/MVVM 在运行时动态加载视图

[英]WPF/MVVM Load a View dynamically at runtime

I have a simple WPF application as below:我有一个简单的 WPF 应用程序,如下所示:

在此处输入图片说明

I also created 3 different views:我还创建了 3 个不同的视图:

  • DetailType1.xaml细节类型1.xaml
  • DetailType2.xaml细节类型2.xaml
  • DetailType3.xaml细节类型3.xaml

and each view has it's own ViewModel每个视图都有自己的 ViewModel

ParentView.xaml父视图.xaml

...
<!-- Detail Area -->
<GroupBox x:Name="groupDetails" Grid.Column="0" Header="Details"
                      HorizontalAlignment="Stretch"
                      Grid.Row="0" VerticalAlignment="Stretch">
   <GroupBox.Resources>
        <ResourceDictionary>
             <DataTemplate DataType="{x:Type vm:DetailType1ViewModel}">
                  <views:DetailType1View/>
             </DataTemplate>
             <DataTemplate DataType="{x:Type vm:DetailType2ViewModel}">
                  <views:DetailType2View/>
             </DataTemplate>
        </ResourceDictionary>
   </GroupBox.Resources>
   <ContentPresenter DataContext="{Binding}" Content="{Binding Path=BaseTypeViewModel}" />
</GroupBox>
...

ParentViewModel.cs父视图模型.cs

...
public BaseViewModel BaseTypeViewModel
{
    get { return GetValue<BaseViewModel>(); }
    set
    {
        SetValue(value);
    }
}

private void ShowDetailDialog()
{
    var vm = GetViewModelByID(SelectedID);
    BaseTypeViewModel = vm;
}

private BaseViewModel GetViewModelByID(int Id)
{
    switch (Id)
    {
        case 1:
            return IoC.Get<DetailType1ViewModel>();
        case 2:
            return IoC.Get<DetailType2ViewModel>();
    }
}
...

DetailType1ViewModel.cs DetailType1ViewModel.cs

public class DetailType1ViewModel : BaseViewModel
{
    ...
}

My question is:我的问题是:

Everytime I double-click row of DataGrid on the left pane, I want to load one of above views into Details area depend on the selected ID.每次双击左侧窗格中的 DataGrid 行时,我想根据所选 ID 将上述视图之一加载到详细信息区域。 So what are the techniques can be used?那么可以使用哪些技术呢? It would be nice if you can show me a code sample.如果您能向我展示代码示例,那就太好了。

Thanks to all for the help.感谢大家的帮助。

  1. Create a <DataTemplate> within a <ResourceDictionary> for each ViewModel that you want to display in the detail area (with the correct DataType={x:Type local:DetailTypeViewModelX} ).<ResourceDictionary>为要在详细信息区域中显示的每个 ViewModel 创建一个<DataTemplate> (使用正确的DataType={x:Type local:DetailTypeViewModelX} )。
  2. Ensure that this <ResourceDictionary> is merged into an ancestor of the details area.确保此<ResourceDictionary>合并到详细信息区域的祖先中。 A possible place would be <Application.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="..." /> .一个可能的位置是<Application.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="..." />
  3. Bind the DataContext of the details area to the instance of the ViewModel you want to display.将详细信息区域的DataContext绑定到要显示的ViewModel 的实例。
  4. Create a <ContentPresenter Content="{Binding}" /> in the details area where you want to display the contents of the <DataTemplate> from step 1.在要显示步骤 1 中<DataTemplate>内容的详细信息区域中创建一个<ContentPresenter Content="{Binding}" />

This should work and follow the overall concept of MVVM.这应该有效并遵循 MVVM 的整体概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM