[英]Binding ContentControl to ViewModel and preserving that VM in the View associated with.
I am creating a WP8 application where I am binding contentcontrol to ViewModel. 我正在创建WP8应用程序,其中将contentcontrol绑定到ViewModel。 This ContentControl takes the DataTemplate specified in App.xaml.cs for that VM and bind to the contentcontrol template.
此ContentControl采用该VM的App.xaml.cs中指定的DataTemplate并绑定到contentcontrol模板。 But the problem is that I am not able to get the instance of that VM in my View.
但是问题是我无法在View中获取该VM的实例。 How can I get or pass my VM instance to my View that is been binded to the content control.
如何获取或传递我的VM实例到绑定到内容控件的View。 Here is the code?
这是代码?
The problem is when DyncmicContentControl gets a ViewModel it calls GetTemplate() method to get the corresponding DataTemplate from App.xaml.cs Which creates a new instance of that View but I am not able to pass this ViewModel to that View. 问题是当DyncmicContentControl获取一个ViewModel时,它将调用GetTemplate()方法从App.xaml.cs获取相应的DataTemplate,这将创建该View的新实例,但我无法将此ViewModel传递给该View。 How can I achieve this??
我该如何实现?
ContentControl.cs ContentControl.cs
public class DynamicContentControl : ContentControl
{
/// <summary>
/// Called when the value of the <see cref="P:System.Windows.Controls.ContentControl.Content" /> property changes.
/// </summary>
/// <param name="oldContent">The old value of the <see cref="P:System.Windows.Controls.ContentControl.Content" /> property.</param>
/// <param name="newContent">The new value of the <see cref="P:System.Windows.Controls.ContentControl.Content" /> property.</param>
protected override void OnContentChanged(object oldContent, object newContent)
{
if (newContent != null)
{
base.OnContentChanged(oldContent, newContent);
this.ContentTemplate = DataTemplateSelector.GetTemplate(newContent);
}
}
}
DataTemplateSelector.cs DataTemplateSelector.cs
/// <summary>
/// Gets the template.
/// </summary>
/// <param name="param">The parameter.</param>
/// <returns></returns>
public static DataTemplate GetTemplate(object param)
{
Type t = param.GetType();
DataTemplate templateData = App.Current.Resources[t.Name] as DataTemplate;
return templateData;
}
MainPage.xaml MainPage.xaml
<Controls:DynamicContentControl Content="{Binding UsrCntrlDynamic}" />
MainPageViewModel.cs MainPageViewModel.cs
public static ObservableCollection<object> ContentControlItems;
public MainPageViewModel()
{
ContentControlItems = new ObservableCollection<object>();
ContentControlItems.Add(new UserControlViewModel());
}
App.xaml 应用程式
<DataTemplate x:Key="UserControlViewModel">
<vm:UserControlView />
</DataTemplate>
A DataTemplate
set on a ContentControl
's ContentTemplate
property is applied to the object that is set on that ContentControl
's Content
property. 在
ContentControl
的ContentTemplate
属性上设置的DataTemplate
将应用于在该ContentControl
的Content
属性上设置的对象。 So in this case setting the ContentTemplate
should render that DataTemplate
with whatever is in the UsrCntrlDynamic
property that you're binding to. 因此,在这种情况下,设置
ContentTemplate
应该使用绑定到的UsrCntrlDynamic
属性中的任何内容呈现该DataTemplate
。 This assumes that the ControlTemplate
for your ContentControl
is set up properly, including a ContentPresenter
to receive and render the Content
, which may not be the case with your custom DynamicContentControl
. 这假定
ContentControl
的ControlTemplate
设置正确,包括一个ContentPresenter
来接收和呈现Content
,而自定义DynamicContentControl
可能不是这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.