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
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.
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.
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
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
;
}
}
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;
Regards,
Petar Mladenov
the Telerik team