C# 如何将viewModel建立到ContentControl

C# 如何将viewModel建立到ContentControl,c#,wpf,mvvm,contentcontrol,C#,Wpf,Mvvm,Contentcontrol,我有一个usercontrol,它定义了一个ContentControl,如下所示: <ContentControl x:Name="PART_contentHost" Grid.Row="1"/> 我错过了什么?我以前做过类似的事情。 这段代码应该让你开始 public void FindAndSetTemplateContent( ContentControl target, ViewModelBase item) { if (target ==

我有一个usercontrol,它定义了一个ContentControl,如下所示:

<ContentControl x:Name="PART_contentHost" Grid.Row="1"/>

我错过了什么?

我以前做过类似的事情。 这段代码应该让你开始

    public void FindAndSetTemplateContent( ContentControl target, ViewModelBase item)
    {
        if (target == null)
            throw new ArgumentNullException("target");

        if (item == null)
            throw new ArgumentNullException("item");

        var template = target.TryFindResource(new DataTemplateKey(item.GetType())) as DataTemplate; // this will pick up your resource for the viewmodel
        if (template == null)
            return null;

        var content = template.LoadContent() as ContentControl ;
        if (content != null)
        {
            content.DataContext = item;
        }
        return content;
    }

我以前做过类似的事情。 这段代码应该让你开始

    public void FindAndSetTemplateContent( ContentControl target, ViewModelBase item)
    {
        if (target == null)
            throw new ArgumentNullException("target");

        if (item == null)
            throw new ArgumentNullException("item");

        var template = target.TryFindResource(new DataTemplateKey(item.GetType())) as DataTemplate; // this will pick up your resource for the viewmodel
        if (template == null)
            return null;

        var content = template.LoadContent() as ContentControl ;
        if (content != null)
        {
            content.DataContext = item;
        }
        return content;
    }

您没有共享足够的代码,我无法确定您正在尝试执行的操作。虽然在某些情况下需要解析模板,但通常有更好的方法。下面是我在MVVM环境下如何理解您的案例,您能这样做吗

Xaml:


您没有共享足够的代码,我无法确定您正在尝试执行的操作。虽然在某些情况下需要解析模板,但通常有更好的方法。下面是我在MVVM环境下如何理解您的案例,您能这样做吗

Xaml:


多说一些问题或什么不起作用。是否出现异常?加载到内容控件的viewmodel/视图的类型是什么。它是用户控件、页面等吗?显示定义和获取“内容”的代码。您的
不设置内容,而是设置
DataContext
。它所做的只是告诉WPF,当它试图将
Test1ViewModel
呈现给VisualTree时,它应该使用
Test1View
绘制它,并将
Test1View
后面的
DataContext
(数据层)设置为ViewModel。详细说明问题或什么不起作用。是否出现异常?加载到内容控件的viewmodel/视图的类型是什么。它是用户控件、页面等吗?显示定义和获取“内容”的代码。您的
不设置内容,而是设置
DataContext
。它所做的只是告诉WPF,当它试图将
Test1ViewModel
呈现给VisualTree时,它应该使用
Test1View
绘制它,并将
Test1View
后面的
DataContext
(数据层)设置为ViewModel。这对于获取模板项也是非常有用的。非常感谢。这对于获取模板项也很好,非常有用。非常感谢。谢谢你的解决方案!!事实上,我使用MVVM,两个答案都是正确的,我错过了一件关于为contentControl的内容建立viewmodel的小事情。谢谢你的解决方案!!事实上,我使用MVVM,这两个答案都是正确的,我错过了一件关于为contentControl的内容建立viewmodel的小事情。
    public void FindAndSetTemplateContent( ContentControl target, ViewModelBase item)
    {
        if (target == null)
            throw new ArgumentNullException("target");

        if (item == null)
            throw new ArgumentNullException("item");

        var template = target.TryFindResource(new DataTemplateKey(item.GetType())) as DataTemplate; // this will pick up your resource for the viewmodel
        if (template == null)
            return null;

        var content = template.LoadContent() as ContentControl ;
        if (content != null)
        {
            content.DataContext = item;
        }
        return content;
    }
<Window.DataContext>
    <local:ViewModel />
</Window.DataContext>
<Window.Resources>
    <DataTemplate DataType="{x:Type local:Test1ViewModel}">
        <local:Test1View />
    </DataTemplate>
</Window.Resources>
<Grid>
    <ContentControl Content="{Binding ContentModel}" />
</Grid>
<UserControl x:Class="WpfApplication1.Test1View" 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <TextBlock Text="{Binding Name}" Background="Beige" Padding="5"  />
        <TextBlock Text="{Binding Address}" Background="PeachPuff" Padding="5" />
    </StackPanel>
</UserControl>
public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    private Test1ViewModel _contentModel;
    public Test1ViewModel ContentModel { get { return _contentModel; } set { _contentModel = value; OnPropertyChanged("ContentModel"); } }

    public ViewModel()
    {
        this.ContentModel = new Test1ViewModel() { Name = "John Higgins", Address = "Wishaw" };
    }

}

public class Test1ViewModel : INotifyPropertyChanged
{
    private string _name;
    public string Name { get { return _name; } set { _name = value; OnPropertyChanged("Name"); } }

    private string _address;
    public string Address { get { return _address; } set { _address = value; OnPropertyChanged("Address"); } }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}