This is a migrated thread and some comments may be shown as answers.

One item template definition for hierachical data template?

1 Answer 123 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Vlada
Top achievements
Rank 1
Vlada asked on 15 May 2020, 02:57 PM

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"
                            />

 

 

 

1 Answer, 1 is accepted

Sort by
0
Vladimir Stoyanov
Telerik team
answered on 20 May 2020, 10:29 AM

Hello Vlada,

Thank you for the shared code snippet. 

If the type of the objects for all levels of the RadTreeView is the same, you can consider using an implicit DataTemplate. This can be achieved by setting the DataType property of the DataTemplate and would ensure that the template is applied to all instances of the specified type. 

I hope you find this information helpful.

Regards,
Vladimir Stoyanov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
General Discussions
Asked by
Vlada
Top achievements
Rank 1
Answers by
Vladimir Stoyanov
Telerik team
Share this question
or