如图样:

View结构

MainView(MainViewModel)
|---Guide1View(Guide1ViewModel)
|---Guide2View(Guide2ViewModel)
  |---Guide2_1View1(Guide2_1ViewModel)
  |---Guide2_1View2(Guide2_1ViewModel)

ViewModel实例结构

Main(ViewModelViewModel)
|---CurrentViewModel(GuidePageViewModelBase)
|---PageViewModelList(ObservableCollection<GuidePageViewModelBase>)
  |---Guide1(Guide1ViewModel)
  |---Guide2(Guide2ViewModel)
    |---LVM1(LViewModel)
    |---LVM2(LViewModel)

1、通过ContentControl显示选中的视图模型对应的视图:

<ContentControl Content="{Binding CurrentViewModel}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type viewmodel:Guide1ViewModel}">
<view:Guide1View/>
</DataTemplate>
<DataTemplate DataType="{x:Type viewmodel:Guide2ViewModel}">
<view:Guide2View/>
</DataTemplate>
</ContentControl.Resources>
</ContentControl>

2、Guide2_1View作为一个多次使用的控件,对应一个LViewModel,设置自己的DataContext(这里的背景是该视图的使用个数确定的。当然也可以用ListBox代替):

<view:Guide2_1View DataContext="{Binding Path=DataContext.LVM1,RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
<view:Guide2_1View DataContext="{Binding Path=DataContext.LVM2,RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />

完整代码:

ViewModel:

using GalaSoft.MvvmLight;

namespace WPF_NestedVMAndView.ViewModel
{
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
PageViewModelList = new ObservableCollection<GuidePageViewModelBase>()
{
new Guide1ViewModel(),
new Guide2ViewModel(),
};
CurrentViewModel = PageViewModelList[];
} public const string CurrentViewModelPropertyName = "CurrentViewModel";
private GuidePageViewModelBase _currentViewModel;
public GuidePageViewModelBase CurrentViewModel
{
get
{
return _currentViewModel;
} set
{
if (_currentViewModel == value)
return; _currentViewModel = value;
RaisePropertyChanged(CurrentViewModelPropertyName);
}
} public const string PageViewModelListPropertyName = "PageViewModelList";
private ObservableCollection<GuidePageViewModelBase> _pageViewModelList;
public ObservableCollection<GuidePageViewModelBase> PageViewModelList
{
get
{
return _pageViewModelList;
} set
{
if (_pageViewModelList == value)
return; _pageViewModelList = value;
RaisePropertyChanged(PageViewModelListPropertyName);
}
}
} public class GuidePageViewModelBase : ViewModelBase
{
public GuidePageViewModelBase(string name)
{
Name = name;
} public const string NamePropertyName = "Name";
private string _name;
public string Name
{
get
{
return _name;
} set
{
if (_name == value)
return; _name = value;
RaisePropertyChanged(NamePropertyName);
}
}
} public class Guide1ViewModel : GuidePageViewModelBase
{
public Guide1ViewModel()
: base("Guide1")
{ }
} public class Guide2ViewModel : GuidePageViewModelBase
{
public Guide2ViewModel() : base("Guide2")
{
LVM1 = new LViewModel()
{
Text = "LVM1",
};
LVM2 = new LViewModel()
{
Text = "LVM2",
};
} public const string LVM1PropertyName = "LVM1";
private LViewModel _lvm1;
public LViewModel LVM1
{
get
{
return _lvm1;
} set
{
if (_lvm1 == value)
return; _lvm1 = value;
RaisePropertyChanged(LVM1PropertyName);
}
} public const string LVM2PropertyName = "LVM2";
private LViewModel _lvm2;
public LViewModel LVM2
{
get
{
return _lvm2;
} set
{
if (_lvm2 == value)
return; _lvm2 = value;
RaisePropertyChanged(LVM2PropertyName);
}
}
} public class LViewModel : ViewModelBase
{
public const string TextPropertyName = "Text";
private string _text;
public string Text
{
get
{
return _text;
} set
{
if (_text == value)
return; _text = value;
RaisePropertyChanged(TextPropertyName);
}
}
}
}

MainView:

<Window x:Class="WPF_NestedVMAndView.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:view="clr-namespace:WPF_NestedVMAndView.View"
xmlns:viewmodel="clr-namespace:WPF_NestedVMAndView.ViewModel"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding Main,Source={StaticResource Locator}}">
<DockPanel Margin="20">
<ListBox ItemsSource="{Binding PageViewModelList}" DisplayMemberPath="Name" SelectedItem="{Binding CurrentViewModel}" DockPanel.Dock="Left" Width="100"/>
<ContentControl Content="{Binding CurrentViewModel}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type viewmodel:Guide1ViewModel}">
<view:Guide1View/>
</DataTemplate>
<DataTemplate DataType="{x:Type viewmodel:Guide2ViewModel}">
<view:Guide2View/>
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
</DockPanel>
</Window>

Guide1View:

<UserControl x:Class="WPF_NestedVMAndView.View.Guide1View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Background="White"
>
<Grid>
<TextBlock Text="{Binding Name}" Margin="10"/>
</Grid>
</UserControl>

Guide2View:

<UserControl x:Class="WPF_NestedVMAndView.View.Guide2View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:view="clr-namespace:WPF_NestedVMAndView.View"
mc:Ignorable="d"
Background="White">
<DockPanel>
<TextBlock Text="{Binding Name}" Margin="10" DockPanel.Dock="Top"/>
<view:Guide2_1View DataContext="{Binding Path=DataContext.LVM1,RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" DockPanel.Dock="Top"/>
<view:Guide2_1View DataContext="{Binding Path=DataContext.LVM2,RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" DockPanel.Dock="Top"/>
</DockPanel>
</UserControl>

Guide2_1View:

<UserControl x:Class="WPF_NestedVMAndView.View.Guide2_1View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Background="White">
<Grid>
<TextBlock Text="{Binding Text}" Margin="10"/>
</Grid>
</UserControl>

WPF中模板选择和DataContext的一些使用的更多相关文章

  1. WPF中如何选择合适的元数据标记?(英文)

    原文:WPF中如何选择合适的元数据标记?(英文) FrameworkPropertyMetadataOptions Enumeration:Specifies the types of framewo ...

  2. 总结:WPF中模板需要绑定父级别的ViewModel该如何处理

    原文:总结:WPF中模板需要绑定父级别的ViewModel该如何处理 <ListBox ItemsSource="{Binding ClassCollection}"> ...

  3. WPF动态模板选择的两种实现

    前言 .net开发工作了六年,看了大量的博客,现在想开始自己写博客,这是我的第一篇博客,试试水,就从自己最常使用的WPF开始. 今天我来给大家分享可用户动态选择控件模板的两种实现方式:DataTrig ...

  4. WPF中 ItemsSource 和DataContext不同点

    此段为原文翻译而来,原文地址 WPF 中 数据绑定 ItemSource和 DataContext的不同点: 1.DataContext 一般是一个非集合性质的对象,而ItemSource 更期望数据 ...

  5. WPF中的数据模板使用方式之一:ContentControl、ContentTemplate和TemplateSelector的使用

    在WPF中,数据模板是非常强大的工具,他是一块定义如何显示绑定的对象的XAML标记.有两种类型的控件支持数据模板:(1)内容控件通过ContentTemplate属性支持数据模板:(2)列表控件通过I ...

  6. wpf 获取datagrid中模板中控件

    //获取name为datagrid中第三列第一行模板的控件 FrameworkElement item = dataGrid.Columns[].GetCellContent(dataGrid.Ite ...

  7. 关于WPF中ItemsControl系列控件中Item不能继承父级的DataContext的解决办法

    WPF中所有的集合类控件,子项都不能继承父级的DataContext,需要手动将绑定的数据源指向到父级控件才可以. <DataGridTemplateColumn Header="操作 ...

  8. WPF 中获取DataGrid 模板列中控件的对像

    WPF 中获取DataGrid 模板列中控件的对像 #region 当前选定行的TextBox获得焦点 /// <summary> /// 当前选定行的TextBox获得焦点 /// &l ...

  9. 在WPF中使用文件夹选择对话框

    开发中有时会想实现"选择某个文件夹"的效果: 在WPF中,使用Microsoft.Win32.OpenFileDialog只能选择文件,FolderBrowserDialog只能用 ...

随机推荐

  1. js 导出Excel

    最近从Silverlight这边转到javascript过来,现在要导出一个导出excel的功能.上级领导指示当页显示多少数据,就导出多少数据,没有必要从后台在去数据.以前也没有接触过这方面的,在网上 ...

  2. Quartz 框架的应用

    本文将简单介绍在没有 Spring 的时候..如何来使用 Quartz... 这里跳过 Quartz 的其他介绍.如果想更加输入的了解 Quartz,大家可以点击下载Quartz的帮助文档. Quar ...

  3. UItableView嵌套UICollectionView

    首先我们需要继承一下UITableView并且遵守<UITableViewDelegate,UITableViewDataSource,UICollectionViewDataSource,UI ...

  4. MDT 2010驱动管理新方法。

    参考:https://4sysops.com/archives/driver-deployment-with-microsoft-deployment-toolkit-mdt-part-1-os-de ...

  5. THEOS makefile

    转自https://www.h4ck.org.cn/2013/07/theos-makefile/ theos的makefile写法与其他linux/unix环境下的makefile写法大同小异,但是 ...

  6. 1.什么是泛型和C#中泛型在Class上的实现

    阅读目录 一:什么是泛型? 二:C#中泛型在Class上的实现   一:什么是泛型? 我们在编程的时候需要一个数据类型,但是在刚开始的时候还不确定这个数据类型是怎么样的,或者说对于不同的多个数据类型有 ...

  7. Tomcat不能自动编译JSP文件问题的一种解决方法

    今天碰到一个非常奇怪的问题,机器环境是JDK8.Tomcat8,把jQuery MiniUI ( for Java Eclipse)下载后导入到Eclipse中,首页可以显示,但运行操作数据库的页面出 ...

  8. C# 读取JSON

    引用 Newtonsoft.Json.dll //C# 读取JSON Newtonsoft.Json.Linq.JObject jsonStr = Newtonsoft.Json.Linq.JObje ...

  9. android 内置视频目录

    在做引导界面的时候有一个视频文件, 把它放在res/raw目录下面. 引用方法 如下: videoView = (VideoView) findViewById(R.id.video_view); v ...

  10. datagridview设置currentrow为指定的某一行[转]

    最近由于程序需要,需要实现指定的行为datagridview的currentrow ,当我设置 dataGridView1.Rows[i].Selected = true时,刷新后,界面显示是当前行被 ...