如果您从UserControl
删除下面的代码片段,并按照我剩下的指示,我相信你会有你想要的东西:
删除此
<UserControl.DataContext>
<viewModels:MyViewModel></viewModels:MyViewModel>
</UserControl.DataContext>
现在,假设你有一个UserControl
或Window
绑定到ViewModel
,你想要代表你ur UserControl
或Window
。你这样做的方式是结合在ResourceDictionary
像这样指定的DataTemplate
使用ContentControl
您UserControl
或Window
内:
.XAML:
<Window x:Class="WPF_Sandbox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:WpfApplication1.ViewModels"
Title="MainWindow"
x:Name="ThisControl">
<Window.DataContext>
<vm:MainWindowViewModel/>
</Window.DataContext>
<DockPanel LastChildFill="True">
<ContentControl DockPanel.Dock="Left" Content="{Binding NavigationRegion}"/>
<ContentControl DockPanel.Dock="Left" Content="{Binding ContentRegion}"/>
</DockPanel>
</Window>
的ContentControl
将隐式的查找与ViewModel
关联的与Content
属性绑定的DataTemplate
(在ResourceDictionary
对象的层次结构中)。因此,可以说,我们在MainWindowViewModel
的ContentRegion
属性设置为您MyViewModel
像这样的实例:
MainWindowViewModel。CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApplication1.ViewModels
{
public class MainWindowViewModel : ViewModel
{
private object _navigationRegion;
private object _contentRegion;
public object NavigationRegion
{
get
{
return _navigationRegion;
}
set
{
_navigationRegion = value;
OnPropertyChanged(nameof(NavigationRegion));
}
}
public object ContentRegion
{
get
{
return _contentRegion;
}
set
{
_contentRegion = value;
OnPropertyChanged(nameof(ContentRegion));
}
}
public MainWindowViewModel()
{
ContentRegion = new MyViewModel(new MyModel());
}
}
}
而且你有像这样一个ResourceDictionary
规定:
MyResourceDictionary.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:vm="clr-namespace:WpfApplication1.ViewModels"
xmlns:views="clr-namespace:WpfApplication1.Views">
<DataTemplate DataType="{x:Type vm:MyViewModel}">
<views:MyView/>
</DataTemplate>
</ResourceDictionary>
而且你已经合并了你的ResourceDictionary
与Application.Resources
在App.xaml文件像所以:
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
该框架将隐式地查找数据类型MyViewModel
的DataTemplate
。该框架将在我们指定的ResourceDictionary
中找到DataTemplate
,MyViewModel
,并看到它应该由MyView
用户控件表示。在这一点上,框架将呈现MyView
用户控件。
和对子孙后代着想:
ViewModel.cs
using System.ComponentModel;
namespace WpfApplication1.ViewModels
{
public abstract class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected string _title;
protected string Title
{
get
{
return _title;
}
set
{
_title = value;
OnPropertyChanged(nameof(Title));
}
}
protected void OnPropertyChanged(string propName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
}
}
请出示一些代码,所以我可以提供帮助。目前还不清楚你的评论是什么问题。 – user1286901
我对此感到抱歉。我不能从办公室发帖。它被锁定。基本上你回答了我的问题。有2个问题。首先,由于定位器用于实例化视图模型,因此使用定位器在视图中绑定上下文也会再次称为viemodel。第二个问题是在内容控制中返回并显示的用户控件中定义数据上下文。所以它被控件和代码调用。再次感谢。 – user953710