Friday, 17 July 2020

WPF Datatemplating an ItemsControl

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<Control>. 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 referring to generic types.

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


Answers:


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


Answers:


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.


Answers:


No comments:

Post a Comment