ContentControl with DataTemplateSelector - need help

I had an insulting problem ... Maybe someone can (please!) Help. I use a model that has and enumerates types and a property that should contain user interface models for each selected type from the enumeration: let it be defined as:

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

I want to have a content control that uses a datatemplateselector to change its appearance every time I change the selected type.

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

PROBLEM: in the DataTemplates that I create to return using ruleEditTemplateSelector DataContext - Type (I agree with this), but I need access to the Model editor to create my DataTemplate ... I don’t know how to deal with it

Thanks in advance!

+5
2

....

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

, , . , , ... Cstein !

+10

, DataTemplate , modifiedModel.

:

Windows.xaml:

<Window.Resources>
    <local:Selector x:Key="sel"/>

    <DataTemplate x:Key="templateA">
        <TextBlock Text="{Binding editedModel.PropertyName}"/>
    </DataTemplate>

    <DataTemplate x:Key="templateB">
        <TextBlock Text="{Binding editedModel.PropertyName}"/>
    </DataTemplate>

    <DataTemplate x:Key="templateC">
        <TextBlock Text="{Binding editedModel.PropertyName}"/>
    </DataTemplate>

</Window.Resources>

Contentcontrol listbox .

DataTemplateSelector:

public class Selector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        if (item is ClassA)
            return (container as FrameworkElement).FindResource("TemplateA") as DataTemplate;
        else if (item is ClassB)
            return (container as FrameworkElement).FindResource("TemplateB") as DataTemplate;
        else if (item is ClassC)
            return (container as FrameworkElement).FindResource("TemplateC") as DataTemplate;
        return null;
    }
}

. , , .

+2

All Articles