Hello Telerik!
I would like to change the HierarchicalDataTemplate based off what level the user is currently on. I am referencing a modified example that was done with the League, Division, Team example.
In my example I would like to have the treeview have the division level use the "OtherDivision" template if there are no items below.
Thanks!
Mark
<Window x:Class="WpfApplication6.MainWindow" |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
xmlns:local="clr-namespace:WpfApplication6" |
xmlns:Telerik="http://schemas.telerik.com/2008/xaml/presentation"> |
<Grid> |
<Grid.Resources> |
<local:LeaguesDataSource x:Key="MyList" /> |
<HierarchicalDataTemplate x:Key="Division" ItemsSource="{Binding Teams}"> |
<StackPanel Orientation="Horizontal"> |
<TextBlock Text="{Binding Name}" FontSize="12" /> |
</StackPanel> |
</HierarchicalDataTemplate> |
<HierarchicalDataTemplate x:Key="OtherDivision" ItemsSource="{Binding Teams}"> |
<StackPanel Orientation="Horizontal"> |
<TextBlock Text="{Binding Name}" FontSize="26" /> |
</StackPanel> |
</HierarchicalDataTemplate> |
<HierarchicalDataTemplate x:Key="League" ItemTemplate="{StaticResource Division}" |
ItemsSource="{Binding Divisions}"> |
<StackPanel Orientation="Horizontal"> |
<TextBlock Text="{Binding Name}" Foreground="Black" FontSize="15" /> |
</StackPanel> |
</HierarchicalDataTemplate> |
<local:LeagueDataTemplateSelector x:Key="myDataTemplateSelector" |
LeagueTemplate="{StaticResource League}" |
DivisionTemplate="{StaticResource Division}" |
OtherDivisionTemplate="{StaticResource OtherDivision}" |
/> |
</Grid.Resources> |
<Grid Width="300" Height="320"> |
<Telerik:RadTreeView |
VerticalAlignment="Top" |
ItemsSource="{Binding Source={StaticResource MyList}}" |
ItemTemplateSelector="{StaticResource myDataTemplateSelector}" |
/> |
</Grid> |
</Grid> |
</Window> |
Code Behind
using System; |
using System.Collections.Generic; |
using System.IO; |
using System.Text; |
using System.Windows; |
using System.Windows.Controls; |
using System.Windows.Data; |
using System.Windows.Documents; |
using System.Windows.Input; |
using System.Windows.Media; |
using System.Windows.Media.Imaging; |
using System.Windows.Shapes; |
using System.Xml.Linq; |
using System.Linq; |
namespace WpfApplication6 |
{ |
/// <summary> |
/// Interaction logic for MainWindow.xaml |
/// </summary> |
public partial class MainWindow : Window |
{ |
public MainWindow() |
{ |
InitializeComponent(); |
} |
} |
public class League |
{ |
public League(string name) |
{ |
_name = name; |
_divisions = new List<Division>(); |
} |
string _name; |
public string Name { get { return _name; } } |
List<Division> _divisions; |
public List<Division> Divisions { get { return _divisions; } } |
} |
public class Division |
{ |
public Division(string name) |
{ |
_name = name; |
_teams = new List<Team>(); |
} |
string _name; |
public string Name { get { return _name; } } |
List<Team> _teams; |
public List<Team> Teams { get { return _teams; } } |
} |
public class Team |
{ |
public Team(string name) |
{ |
_name = name; |
} |
string _name; |
public string Name { get { return _name; } } |
} |
public class LeaguesDataSource : List<League> |
{ |
public LeaguesDataSource() |
{ |
League leagueA = new League("League A"); |
League leagueB = new League("League B"); |
Division leagueADivisionA = new Division("Division A"); |
leagueA.Divisions.Add(leagueADivisionA); |
Division leagueADivisionB = new Division("Division B"); |
leagueA.Divisions.Add(leagueADivisionB); |
Division leagueADivisionC = new Division("Division C"); |
leagueA.Divisions.Add(leagueADivisionC); |
Division leagueBDivisionA = new Division("Division A"); |
leagueBDivisionA.Teams.Add(new Team("Team 1")); |
leagueBDivisionA.Teams.Add(new Team("Team 2")); |
leagueBDivisionA.Teams.Add(new Team("Team 3")); |
leagueBDivisionA.Teams.Add(new Team("Team 4")); |
leagueBDivisionA.Teams.Add(new Team("Team 5")); |
leagueB.Divisions.Add(leagueBDivisionA); |
Division leagueBDivisionB = new Division("Division B"); |
leagueBDivisionB.Teams.Add(new Team("Team Diamond")); |
leagueBDivisionB.Teams.Add(new Team("Team Heart")); |
leagueBDivisionB.Teams.Add(new Team("Team Club")); |
leagueBDivisionB.Teams.Add(new Team("Team Spade")); |
leagueB.Divisions.Add(leagueBDivisionB); |
Division leagueBDivisionC = new Division("Division C"); |
leagueBDivisionC.Teams.Add(new Team("Team Alpha")); |
leagueBDivisionC.Teams.Add(new Team("Team Beta")); |
leagueBDivisionC.Teams.Add(new Team("Team Gamma")); |
leagueBDivisionC.Teams.Add(new Team("Team Delta")); |
leagueBDivisionC.Teams.Add(new Team("Team Epsilon")); |
leagueB.Divisions.Add(leagueBDivisionC); |
this.Add(leagueA); |
this.Add(leagueB); |
} |
} |
public class LeagueDataTemplateSelector : DataTemplateSelector |
{ |
private HierarchicalDataTemplate leagueTemplate; |
private HierarchicalDataTemplate divisionTemplate; |
private HierarchicalDataTemplate otherDivisionTemplate; |
public LeagueDataTemplateSelector() |
{ |
} |
public override DataTemplate SelectTemplate(object item, DependencyObject container) |
{ |
if (item is League) |
return this.leagueTemplate; |
else if (item is Division) |
return this.divisionTemplate; |
return null; |
} |
public HierarchicalDataTemplate LeagueTemplate |
{ |
get |
{ |
return this.leagueTemplate; |
} |
set |
{ |
this.leagueTemplate = value; |
} |
} |
public HierarchicalDataTemplate DivisionTemplate |
{ |
get |
{ |
return this.divisionTemplate; |
} |
set |
{ |
this.divisionTemplate = value; |
} |
} |
public HierarchicalDataTemplate OtherDivisionTemplate |
{ |
get |
{ |
return this.otherDivisionTemplate; |
} |
set |
{ |
this.otherDivisionTemplate = value; |
} |
} |
} |
} |