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

Change HierarchicalDataTemplate programmatically

4 Answers 222 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Meerkat
Top achievements
Rank 1
Meerkat asked on 29 May 2009, 10:28 AM

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.

4 Answers, 1 is accepted

Sort by
0
Accepted
Tihomir Petkov
Telerik team
answered on 29 May 2009, 01:25 PM
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.
0
Meerkat
Top achievements
Rank 1
answered on 29 May 2009, 02:01 PM
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.
0
Olga
Top achievements
Rank 1
answered on 12 Mar 2011, 10:39 PM
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

0
Petar Mladenov
Telerik team
answered on 16 Mar 2011, 04:20 PM
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!
Tags
TreeView
Asked by
Meerkat
Top achievements
Rank 1
Answers by
Tihomir Petkov
Telerik team
Meerkat
Top achievements
Rank 1
Olga
Top achievements
Rank 1
Petar Mladenov
Telerik team
Share this question
or