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 コンテキストであなたのケースを理解する方法は次のとおりです。このようにできますか?

ここに画像の説明を入力

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>

ビューモデル:

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 に答える