ContentControl with DataTemplateSelector - help needed

Victor picture Victor · May 25, 2012 · Viewed 13.5k times · Source

I got an enoying problem... Maybe someone can (please!) help. I am using a model that has and enumeration of types and a property that should hold UI models for each selected type from enumeration: Let's define them like:

class ViewModel
   {
     Types selectedType{get;set;}
     UiModelBase editedModel{get;set;}
   }

I want to have a content control that use datatemplateselector to change his view each time I change the selectedType.

    <ListBox x:Name="RuleTypeList" ItemsSource="{Binding Source={StaticResource Types}}" SelectedItem="{Binding Path=selectedType}"/>     
    <!--Content control-->
    <ContentControl ContentTemplateSelector="{StaticResource ruleEditTemplateSelector}" 
             Content="{Binding SelectedItem, ElementName=RuleTypeList}"/>

the PROBLEM: In DataTemplates that I create to be returned by ruleEditTemplateSelector the DataContext is Type (agree with that) but I need access to the editedModel to create my DataTemplate...I do not know how to deal with it

Thanks in advance!

Answer

Victor picture Victor · May 31, 2012

The solution was not very hard....

 DataContext="{Binding RelativeSource={RelativeSource AncestorType=ContentControl},Path=DataContext}"

In this way the context of template is the same with the content of his parent and I can acces his members. I think I tryind to do that but I didn't code correct... Thanks Cstein for involving !