[英]How to establish a viewModel to a ContentControl
我有一個用戶控件,它定義了一個ContentControl,如下所示:
<ContentControl x:Name="PART_contentHost" Grid.Row="1"/>
在viewmodel中,我將獲得一個viewModel,它將顯示在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
我想念什么?
我以前做過這樣的事情。 此代碼應使您入門。
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:
<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));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.