-1

I have two HierarchicalDataTemplates that are identical, except for the ConverterParameter used for the ItemsSource property:

<HierarchicalDataTemplate x:Key ="Template1" 
    ItemsSource="{Binding Converter ={StaticResource DemoConverter}, ConverterParameter='One'}">
  ...
</HierarchicalDataTemplate>

<HierarchicalDataTemplate x:Key ="Template2" 
    ItemsSource="{Binding Converter ={StaticResource DemoConverter}, ConverterParameter='Two'}">
   ...
</HierarchicalDataTemplate>

How can I avoid the redundancy (replaced above by ...)? I would like to have only one template that I can pass the (static) ConverterParameter to. Something like this (with PARAMETER placeholder):

<HierarchicalDataTemplate x:Key ="Template" 
    ItemsSource="{Binding Converter ={StaticResource DemoConverter}, ConverterParameter={PARAMETER}">
   ...
</HierarchicalDataTemplate>

And, in the "consuming" code I would write something like (with another PARAMETER placeholder):

<TreeView ItemTemplate="{StaticResource ProjectTreeEditItem, PARAMETER=One}" ...></TreeView>    

or

<TreeView ItemTemplate="{StaticResource ProjectTreeEditItem, PARAMETER=Two}" ...></TreeView>    

How can this be achieved? A suitable (although sub-optimal) solution would be to have two "wrapper" templates that reference one common template (setting the parameter from there, if setting the parameter as part of the ItemTemplate definition is the problem), but I wouldn't know how to do that either :o/

mike
  • 1,627
  • 1
  • 14
  • 37

1 Answers1

0

How can this be achieved?

By defining the templates programmatically using the XamlReader.Load method or the FrameworkElementFactory class. Please see my answer here for an example:

Combining DataTemplates at runtime

You can't do something like "pass the (static) ConverterParameter" to another template in XAML. I am afraid this is not supported by the markup language.

mm8
  • 163,881
  • 10
  • 57
  • 88