1

我有一个用户控件,它定义了这样的 ContentControl:

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

在视图模型中,我将获得一个视图模型,它将显示在 contentControl 中。为了建立与视图的链接,我有一个数据模板来建立它们之间的关系。

<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

我错过了什么?

4

2 回答 2

1

您没有共享足够的代码让我确定您正在尝试做什么。虽然在某些情况下您需要解析模板,但通常有更好的方法。所以这就是我在 MVVM 上下文中如何理解你的情况,你能这样做吗?

在此处输入图像描述

xml:

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

测试1视图:

<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));
    }
}
于 2013-03-18T19:04:37.117 回答
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;
    }
于 2013-03-18T17:20:13.607 回答