ContentControl with DataTemplateSelector - help needed
Asked Answered
U

2

6

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!

Unbrace answered 25/5, 2012 at 8:57 Comment(0)
U
14

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 !

Unbrace answered 31/5, 2012 at 7:23 Comment(1)
thanks! I was looking for this solution for a long time.Edirne
B
2

If I understand you right you want to create your DataTemplate inside the templateselector, while the datatemplate is based on the editedModel property.

I would solve this problem this way:

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 and listbox stay the same.

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;
    }
}

This return an existing datatemplate depending on the item's type. I hope I understood you right and it helps you.

Blacklist answered 25/5, 2012 at 9:38 Comment(5)
No...this is not my problem. I know the workflow. the problem is inside templateA, B,C. because editedModel.PropertyName is not found. (the data context is selectedType...)Unbrace
If your Window.xaml's datacontext is your viewmodel and you put your datatemplates into Window.Resources tag then you can access your editedModel.PropertyName from your DataTemplateBlacklist
Yes I have the DataTemplate in Windows.Resources and no...unfortunately it said it does not find any: editedModel.PropertyName in sectedType ....witch is true...Unbrace
Yes it finds no property with that name as long as you don't declare it. Or did I missunderstand you?Blacklist
I'm affraid I might not understand what you are saying... an instance of editedModel.PropertyName it already exists in code behind...why should I declare it again...I want to alter the instance that already exist. I am understanding wrong?Unbrace

© 2022 - 2024 — McMap. All rights reserved.