views:

3434

answers:

3

I have an ItemsControl whose ItemsSource gets bound to an ObservableCollection<Component> at run time. I have defined a data template for type Component which works fine.

Now Component has a ObservableCollection<Control> and I want to add another ItemsControl inside my Component Datatemplate to render all the controls. Control here is my own custom object not related to a wpf control.

There are different types of controls so I am trying to use an ItemTemplateSelector to select the right template for each type. In the example below to keep it small I have only shown one of the templates "RWString" which I find using a FindResource in MyControlTemplateSelector overriding SelectTemplate. But the SelectTemplate never gets called(using a breakpoint to check). Is there something wrong in my xaml?

<ItemsControl.Resources>
    <src:MyControlTemplateSelector x:Key="XSelector" />
    <DataTemplate DataType="{x:Type src:Component}"  >
        <Expander Visibility="{Binding Path=Show}">
                <ItemsControl ItemsSource="{Binding Path=Contrls}" 
                          ItemTemplateSelector="{StaticResource XSelector}">
                <ItemsControl.Resources>
                    <DataTemplate x:Key="RWstring" >
                        <TextBlock Text="{Binding Path=Label}"/>
                    </DataTemplate>
                </ItemsControl.Resources>
                <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate><WrapPanel /></ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </Expander>
    </DataTemplate>
</ItemsControl.Resources>

Update: Contrls is not a typo, its just me using a silly naming system. Contrls is a property of Component of type ObservableCollection. Also the reason I am trying to use the the ItemsTemplateSelector is that the ObservableCollection<Control> contains objects of generic types like Control<int> Control<string> etc all deriving from Control and apparently you cant create datatemplates refering to generic types.

Update3: Removed update 2 as it was unrelated. I got the ItemTemplateSelector to work by changing StaticResource to DynamicResource. But I dont know why this works...

A: 

In the line where you bind the nested ItemsControl, is the Path correct? It is currently "Contrls", should it be "Controls"?

Ray Booysen
It is contrls. I need to change that name though...it is confusing.
Klerk
A: 

Can you post some code relating to the data you're binding to?

Ray Booysen
Do you see a problem having a datatemplate within another datatemplate
Klerk
+1  A: 

I'm guessing this doesn't work with a StaticResource as the Resource is inside the ItemsControl which probably has not been created at load time when StaticResources are evaluated.

DynamicResources at load time are evaluated to an expression at load time and then evaluated to the correct value when requested.

Try move the Resource outside of the ItemsControl.

Ray Booysen
Thanks that was the problem. I was creating it inside the itemscontrol that was being created at runtime
Klerk