如何建立到ContentControl的viewModel

时间:2013-03-18 16:17:07

标签: c# wpf mvvm contentcontrol

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

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

在viewmodel中,我将获得一个将在contentControl中显示的viewModel。为了与视图建立链接,我有一个datatemplate来建立它们之间的关系。

<DataTemplate DataType="{x:Type ViewModels:Test1ViewModel}">
        <Views:Test1View />
</DataTemplate>

这意味着我希望Test1ViewModel显示在contentControl中。我无法在我的代码C#中坚持这一点。

//this gets the contentControl from de template
contentHost = this.Template.FindName(contentHostName, this) as ContentControl; 
//this assigns the test1ViewModel
contentHost.Content = content

我错过了什么?

2 个答案:

答案 0 :(得分:0)

我以前做过这样的事情。 这段代码应该让你入门。

    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;
    }

答案 1 :(得分:0)

您还没有为我分享足够的代码以确定您要执行的操作。虽然在某些情况下您需要解析模板,但最常见的是有更好的方法。所以这就是我在MVVM上下文中理解你的情况,你能这样做吗?

enter image description here

的Xaml:

<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>

Test1View:

<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>

的ViewModels:

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));
    }
}