In converting my treeview to use checkboxes, as in the article by Josh Smith, I find my working xaml code to use multiple HierarchicalDataTemplates of exactly the same format, but with different DataTypes.
Is there anyway to define a single static HierarchicalDataTemplate in XAML, but assign different datatypes where the xaml instantiates the objects?
Here are two working HierarchicalDataTemplates. The only diffence is in the DataType:
XAML
<HierarchicalDataTemplate
DataType="{x:Type r:ReportViewModel}"
ItemsSource="{Binding Children}"
>
<StackPanel Orientation="Horizontal">
<CheckBox
Focusable="False"
IsChecked="{Binding IsChecked}"
VerticalAlignment="Center"
/>
<ContentPresenter
Content="{Binding Name, Mode=OneTime}"
Margin="2,0"
/>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate
DataType="{x:Type r:NetworkViewModel}"
ItemsSource="{Binding Children}"
>
<StackPanel Orientation="Horizontal">
<CheckBox
Focusable="False"
IsChecked="{Binding IsChecked}"
VerticalAlignment="Center"
/>
<ContentPresenter
Content="{Binding Name, Mode=OneTime}"
Margin="2,0"
/>
</StackPanel>
</HierarchicalDataTemplate>
Thanks for any help.
Addendum: I found a partial answer at How do I reuse a HierarchicalDataTemplate?
Unless someone has a better solution?
Another option would be to implement a base class which has the properties as bound to by the TreeView.
e.g.
NB: Not showing full
INotifyPropertyChanged
implementation.Bind any derived class objects to a
TreeView.ItemsSource
as a collection of the type of the base class which should mean yourHierarchicalDataTemplate
for the base class will be picked up by the WPF Type Inference system.e.g.