One item template definition for hierachical data template?

2 posts, 0 answers
  1. Vlada
    Vlada avatar
    2 posts
    Member since:
    Nov 2016

    Posted 15 May Link to this post

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

     

     

     

  2. Vladimir Stoyanov
    Admin
    Vladimir Stoyanov avatar
    679 posts

    Posted 20 May Link to this post

    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.
Back to Top