Change HierarchicalDataTemplate programmatically

5 posts, 1 answers
  1. Meerkat
    Meerkat avatar
    64 posts
    Member since:
    Feb 2007

    Posted 29 May 2009 Link to this post

    Hello,

    Is it possible to change a HierarchicalDataTemplate programmatically?

    I have looked at one or two examples involving DataTemplateSelector but I don't think that is what I want.

    I have two different HierarchicalDataTemplates in the <UserControl.Resources> section
    and I simply want to select one of them depending on the Role of the user.

    Any help or guidance would be very much appreciated,

    Many thanks,

    Pete.

  2. Answer
    Tihomir Petkov
    Admin
    Tihomir Petkov avatar
    576 posts

    Posted 29 May 2009 Link to this post

    Hi Pete,

    Can you please elaborate a bit on why you don't think the DataTemplateSelector is not suitable for your scenario? Its sole purpose is to do what you are saying you want to do - select a data template based on some custom logic. Please take a look at the attached sample project I prepared for you and in case it differs from what you want to achieve let me know.

    Of course, you can manipulate the HierarchicalDataTemplates from the code-behind, but that would be much more cumbersome and certainly less elegant than the aproach demonstrated in the attached sample.

    All the best,
    Tihomir Petkov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. DevCraft banner
  4. Meerkat
    Meerkat avatar
    64 posts
    Member since:
    Feb 2007

    Posted 29 May 2009 Link to this post

    Hello Tihomir,

    You have been unbelievably helpful, thankyou.

    Your sample project was EXACTLY what I wanted.

    I fear my initial attempts at understanding how to use DataTemplateSelector, were obviously inadequate.

    After closely examining your sample, all is now perfectly clear.

    I would like to say that I am more than impressed by the excellent help given by Telerik.

    Once again, sincere thanks,

    Pete.
  5. Olga
    Olga avatar
    14 posts
    Member since:
    Mar 2011

    Posted 12 Mar 2011 Link to this post

    I didn't quite understand your example.
    I do this in xaml:

    <

     

     

    telerik:RadTreeView SelectionChanged="TasksTreeView_SelectionChanged" Name="TasksTreeView" ItemTemplate="{StaticResource SpecDataTemplate}"/>

    how can I do TasksTreeView.ItemTemplate = TotalDataTemplate

    in c# code?

    I have the following templates:

     

     

     

     

     

    <telerik:HierarchicalDataTemplate x:Key="SpecDataTemplate" ItemsSource="{Binding Children}">

     

     

     

     

     

     

     

     

     

     

     

     

    <StackPanel Orientation="Horizontal">

     

     

     

     

     

     

     

     

     

     

     

     

    <TextBlock Text="{Binding ItemID}" />

     

     

     

     

     

     

     

     

     

     

     

     

    <TextBlock Text="{Binding Title}" Margin="5,0,0,0" />

     

     

     

     

     

     

     

     

     

     

     

     

    </StackPanel>

     

     

     

     

     

     

     

     

     

     

     

     

    </telerik:HierarchicalDataTemplate>

    And

     

     

     

     

     

     

     

     

     

     

     

     

    <telerik:HierarchicalDataTemplate x:Key="TotalDataTemplate" ItemsSource="{Binding Children}">

     

     

     

     

     

     

     

     

     

     

     

     

    <StackPanel Orientation="Horizontal">

     

     

     

     

     

     

     

     

     

     

     

     

    <TextBlock Text="{Binding ItemID}" />

     

     

     

     

     

     

     

     

     

     

     

     

    <TextBlock Text="{Binding Title}" Margin="5,0,0,0" />

     

     

     

     

     

     

     

     

     

     

     

     

    <TextBlock Text="{Binding Description}" Margin="5,0,0,0" />

     

     

     

     

     

     

     

     

     

     

     

     

    <TextBlock Text="{Binding Priority}" Margin="5,0,0,0" />

     

     

     

     

     

     

     

     

     

     

     

     

    <TextBlock Text="{Binding Scope}" Margin="5,0,0,0" />

     

     

     

     

     

     

     

     

     

     

     

     

    <TextBlock Text="{Binding SDTotalTime}" Margin="5,0,0,0" />

     

     

     

     

     

     

     

     

     

     

     

     

    <TextBlock Text="{Binding PMTotalTime}" Margin="5,0,0,0" />

     

     

     

     

     

     

     

     

     

     

     

     

    <TextBlock Text="{Binding UXTotalTime}" Margin="5,0,0,0" />

     

     

     

     

     

     

     

     

     

     

     

     

    <TextBlock Text="{Binding DevTotalTime}" Margin="5,0,0,0" />

     

     

     

     

     

     

     

     

     

     

     

     

    <TextBlock Text="{Binding QATotalTime}" Margin="5,0,0,0" />

     

     

     

     

     

     

     

     

     

     

     

     

    <TextBlock Text="{Binding OpsTotalTime}" Margin="5,0,0,0" />

     

     

     

     

     

     

     

     

     

     

     

     

    <TextBlock Text="{Binding TotalTaskTime}" Margin="5,0,0,0" />

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    </StackPanel>

    Thanks

  6. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 16 Mar 2011 Link to this post

    Hi Meerkat,

    Please examine the attached project where a TemplateSelector class is Used.
    public class SampleTemplateSelector : DataTemplateSelector
        {
            public override DataTemplate SelectTemplate(object item, DependencyObject container)
            {
                if (item is User)
                    return this.UserTemplate;
                else if (item is BusinessItem)
                    return this.BusinessTemplate;
                else
                    return new DataTemplate();
            }
            public DataTemplate UserTemplate
            {
                get;
                set;
            }
            public DataTemplate BusinessTemplate
            {
                get;
                set;
            }
        }
    This class have DataTemplates as properties and its method SelectTemplate() is fired when every RadTreeViewItem is being generated. In "item", the business object inside the tree item is passed and the template is selected corresponding to the type of the business object.
    However, if you need to set an ItemTemplate on a RadTreeView from code behind you can do like so:
    this.treeView.ItemTemplate = this.LayoutRoot.Resources["SpecDataTemplate"] as HierarChicalDataTemplate;
    Feel free to ask if you need more info on this.


    Regards,
    Petar Mladenov
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Back to Top
DevCraft banner