I do not know if I have defined question correctly, sorry for my lack of experience. Application I work on have a TreeView that is connected to data source. Data is hierarchical. There are root element and every root element can have children. Any children can have multiple other children. I have figured out how to data bind and show hierarchical data. Problem is that I need to style treeview items. I have figured out how to set itemtemplate on root and children in xaml. But now I have two copies of item template. One in ItemTemplate and another in its's hierachical item template. But they are identical, as I understand MVVM this could be an issue when I try to implement something more complex. Is there some elegant way to have this defined in one place and then shared on item template root and then it's children?
Here is the code of my TreeView in XAML:
<
HierarchicalDataTemplate
x:Key
=
"HostsTemplatekey"
ItemsSource
=
"{Binding Children}"
>
<
StackPanel
Orientation
=
"Horizontal"
telerik:RadContextMenu.ContextMenu
=
"{StaticResource TreeViewContextMenu}"
>
<
iconPacks:PackIconFeatherIcons
Kind
=
"Trash2"
Width
=
"20"
Height
=
"20"
Visibility
=
"{Binding Converter={StaticResource FileTypeToIconPackVisibilityConverter}, ConverterParameter= 3}"
/>
<
TextBlock
Width
=
"10"
/>
<
Grid
>
<
TextBlock
Text
=
"{Binding cityname}"
VerticalAlignment
=
"Center"
FontSize
=
"{StaticResource BaseCaptionFontSize }"
/>
</
Grid
>
</
StackPanel
>
<
HierarchicalDataTemplate.ItemTemplate
>
<
HierarchicalDataTemplate
ItemsSource
=
"{Binding Children}"
>
<
StackPanel
Orientation
=
"Horizontal"
telerik:RadContextMenu.ContextMenu
=
"{StaticResource TreeViewContextMenu}"
>
<
iconPacks:PackIconFeatherIcons
Kind
=
"Trash2"
Width
=
"20"
Height
=
"20"
Visibility
=
"{Binding Converter={StaticResource FileTypeToIconPackVisibilityConverter}, ConverterParameter= 3}"
/>
<
TextBlock
Width
=
"10"
/>
<
Grid
>
<
TextBlock
Text
=
"{Binding cityname}"
VerticalAlignment
=
"Center"
FontSize
=
"{StaticResource BaseCaptionFontSize }"
/>
</
Grid
>
</
StackPanel
>
</
HierarchicalDataTemplate
>
</
HierarchicalDataTemplate.ItemTemplate
>
</
HierarchicalDataTemplate
>
<
telerik:RadTreeView
Name
=
"dgCities"
Margin
=
"10"
ItemsSource
=
"{Binding data.CitiesCollection}"
IsEditable
=
"True"
ItemTemplate
=
"{StaticResource HostsTemplatekey}"
SelectedItem
=
"{Binding data.SelectedCity, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
IsDragDropEnabled
=
"True"
/>