简体   繁体   中英

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. This ContentControl takes the DataTemplate specified in App.xaml.cs for that VM and bind to the contentcontrol template. But the problem is that I am not able to get the instance of that VM in my View. How can I get or pass my VM instance to my View that is been binded to the content control. 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. How can I achieve this??

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

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

    <Controls:DynamicContentControl Content="{Binding UsrCntrlDynamic}" />

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. So in this case setting the ContentTemplate should render that DataTemplate with whatever is in the UsrCntrlDynamic property that you're binding to. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM