I want to use a HierarchicalDataTemplate with a DataTemplateSelector but I get trouble with the order:
<UserControl.Resources>
<HierarchicalDataTemplate x:Key="TemplateA"
ItemsSource="{Binding AnySource}"
ItemTemplateSelector="{StaticResource MyTemplateSelector}" >
<Label Content="A" />
</HierarchicalDataTemplate>
<DataTemplate x:Key="TemplateB">
<Label Content="B" />
</DataTemplate>
<viewmodel:MyTemplateSelector
TemplateA="{StaticResource TemplateA}"
TemplateB="{StaticResource TemplateB}"
x:Key="MyTemplateSelector" />
<HierarchicalDataTemplate x:Key="TemplateC"
ItemsSource="{Binding AnotherSource}"
ItemTemplateSelector="{StaticResource MyTemplateSelector}">
<Label Content="C" />
</HierarchicalDataTemplate>
</UserControl.Resources>
<Grid>
<TreeView
ItemsSource="{Binding Source={StaticResource SomeList}}"
ItemTemplate="{StaticResource TemplateC}"/>
</Grid>
MyTemplateSelector depends on TemplateA and TemplateA depends on MyTemplateSelector. I get a System.Windows.Markup.XamlParseException at Runtime. Is there a way of forward declaration in XAML or is there a different solution?
Solution: I've found the Solution: I don't need to set the TemplateSelector in TemplateA because it is set in the parent DataTemplate. Sometimes it can be so simple...
[It seems that you overcame the issue by not loading the resource. I'll still post the answer for future readers]
You can use DynamicResource
instead of StaticResource
.
This only works if you populate a dependency property, i.e. Binding.Converter="{DynamicResource MyConverter}"
will not work since Binding.Converter
is not a dependency property.