Limiting tree node depth

3 posts, 1 answers
  1. Justin Lee
    Justin Lee avatar
    149 posts
    Member since:
    Dec 2009

    Posted 06 Jan 2011 Link to this post

    I have 2 treeviews and both are bound to the same datasource.  In one tree, I want to show the full hierarchy of nodes, but in the other tree I just want to show the first 2 levels of nodes.  Is there anyway I can limit the number of levels a TreeView will display?  I would perfer to not have 2 separate datasources. (there are actions that update the datasource, and I don't want to have to update both datasources all the time)

    Thanks,
    Justin
  2. Answer
    Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 06 Jan 2011 Link to this post

    Hi Justin Lee,

    When the RAdTreeView control is data bound, you can control the vizualization of its data source through the ItemTemplate property. When you define a HierarchicalDataTemplate, the TreeView will visualize its data hierarchicly. However, when you define a DataTemplate the data will be displayed as flat collection. Therefore, in your case you can defiene one HierarchicalTemplate for the second treeview and set its ItemTemplate to a DataTemplate, for example like so:
    <UserControl.Resources>
           <DataTemplate x:Key="SecondLevelTemplate">
               <TextBlock Text="{Binding MyValue}" />
           </DataTemplate>
           <telerik:HierarchicalDataTemplate x:Key="FirstLevelTemplate"
                                             ItemsSource="{Binding Items}"
                                             ItemTemplate="{StaticResource SecondLevelTemplate}">
               <TextBlock Text="{Binding MyValue}" />
           </telerik:HierarchicalDataTemplate>
       </UserControl.Resources>
       <Grid x:Name="LayoutRoot"
             Background="White">
           <telerik:RadTreeView ItemsSource="{Binding MyData}"
                                ItemTemplate="{StaticResource FirstLevelTemplate}" />
       </Grid>
    This way the second-level data will be display as a flat collection.

    Give this a try and let us know if it works for you.

    All the best,
    Tina Stancheva
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for Silverlight
  3. DevCraft banner
  4. Justin Lee
    Justin Lee avatar
    149 posts
    Member since:
    Dec 2009

    Posted 06 Jan 2011 Link to this post

    That worked perfectly. Thanks!
Back to Top