XAML:
<Window x:Class="MyTreeView.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:MyTreeView"
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
<Grid Width="281">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<Grid.Resources>
<my:ListLeague x:Key="MyList"/>
<HierarchicalDataTemplate DataType = "{x:Type my:League}"
ItemsSource = "{Binding Path=Divisions}">
<TextBlock Text="{Binding Path=LeagueName}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType= "{x:Type my:Division}"
ItemsSource = "{Binding Path=Teams}">
<TextBlock Text="{Binding Path=DivisionName}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType= "{x:Type my:Team}"
ItemsSource = "{Binding Path=Players}">
<TextBlock Text="{Binding Path=TeamName}"/>
</HierarchicalDataTemplate>
<!--<DataTemplate DataType="{x:Type my:Player}">
<TextBlock Text="{Binding Path=FName}"/>
</DataTemplate>-->
</Grid.Resources>
<TreeView x:Name="tvLeague" Grid.Row="0" SelectedItemChanged="tvLeague_SelectedItemChanged">
<!-- <TreeViewItem ItemsSource="{Binding Source={StaticResource MyList}}" Header="My Soccer Leagues" />-->
</TreeView>
<StackPanel Grid.Row="1" Orientation="Horizontal">
<Button Width="80" Height="30" Click="Button_Click" Content="Add Zone" />
<Button Width="80" Height="30" Click="Button_Click_1" Content="Add Team"/>
</StackPanel>
</Grid>
</Window>
Code :
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
namespace MyTreeView
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
private ObservableCollection<League> lstLeague = new ObservableCollection<League>();
public League CurrentLeague { get; set; }
public Division CurrentDivision { get; set; }
public Team CurrentTeam { get; set; }
private int j = 0;
public Window1()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
lstLeague.ToList().Find(i => i.LeagueName == CurrentLeague.LeagueName).
Divisions.Add(new Division { DivisionName = string.Format("New Zone {0}", j++), Teams = new ObservableCollection<Team>() });
}
private void tvLeague_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
if (((TreeView)sender).SelectedItem.GetType().Equals(typeof(League)))
{
League league = ((TreeView)sender).SelectedItem as League;
CurrentLeague = league;
}
else if (((TreeView)sender).SelectedItem.GetType().Equals(typeof(Division)))
{
Division division = ((TreeView)sender).SelectedItem as Division;
CurrentDivision = division;
}
else if (((TreeView)sender).SelectedItem.GetType().Equals(typeof(Team)))
{
Team team = ((TreeView)sender).SelectedItem as Team;
CurrentTeam = team;
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
League l;
Division d;
Division d1; Division d2; Division d3; Team t1; Team t2; Team t3; Team t4;
Player p1; Player p2; Player p3; Player p4;
lstLeague.Add(l = new League { LeagueName = "IPL", Divisions = new ObservableCollection<Division>() });
d = new Division { DivisionName = "EAST", Teams = new ObservableCollection<Team>() };
d1 = new Division { DivisionName = "WEST", Teams = new ObservableCollection<Team>() };
d2 = new Division { DivisionName = "SOUTH", Teams = new ObservableCollection<Team>() };
d3 = new Division { DivisionName = "NORTH", Teams = new ObservableCollection<Team>() };
l.Divisions.Add(d1);
l.Divisions.Add(d2);
l.Divisions.Add(d3);
l.Divisions.Add(d);
t1 = new Team { TeamName = "MI", Players = new ObservableCollection<Player>() };
t2 = new Team { TeamName = "KKR", Players = new ObservableCollection<Player>() };
t3 = new Team { TeamName = "RR", Players = new ObservableCollection<Player>() };
t4 = new Team { TeamName = "CS", Players = new ObservableCollection<Player>() };
d.Teams.Add(t1);
d1.Teams.Add(t2);
d2.Teams.Add(t3);
d3.Teams.Add(t4);
p1 = new Player { FName = "P1", LName = "p1" };
p2 = new Player { FName = "P1", LName = "p1" };
p3 = new Player { FName = "P1", LName = "p1" };
p4 = new Player { FName = "P1", LName = "p1" };
t1.Players.Add(p1);
t2.Players.Add(p2);
t3.Players.Add(p3);
t4.Players.Add(p4);
lstLeague.Add(l = new League { LeagueName = "ICL", Divisions = new ObservableCollection<Division>() });
d = new Division { DivisionName = "EAST", Teams = new ObservableCollection<Team>() };
d1 = new Division { DivisionName = "WEST", Teams = new ObservableCollection<Team>() };
d2 = new Division { DivisionName = "SOUTH", Teams = new ObservableCollection<Team>() };
d3 = new Division { DivisionName = "NORTH", Teams = new ObservableCollection<Team>() };
l.Divisions.Add(d1);
l.Divisions.Add(d2);
l.Divisions.Add(d3);
l.Divisions.Add(d);
t1 = new Team { TeamName = "MI", Players = new ObservableCollection<Player>() };
t2 = new Team { TeamName = "KKR", Players = new ObservableCollection<Player>() };
t3 = new Team { TeamName = "RR", Players = new ObservableCollection<Player>() };
t4 = new Team { TeamName = "CS", Players = new ObservableCollection<Player>() };
d.Teams.Add(t1);
d1.Teams.Add(t2);
d2.Teams.Add(t3);
d3.Teams.Add(t4);
p1 = new Player { FName = "P1", LName = "p1" };
p2 = new Player { FName = "P1", LName = "p1" };
p3 = new Player { FName = "P1", LName = "p1" };
p4 = new Player { FName = "P1", LName = "p1" };
t1.Players.Add(p1);
t2.Players.Add(p2);
t3.Players.Add(p3);
t4.Players.Add(p4);
tvLeague.ItemsSource = lstLeague;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
lstLeague.ToList().Find(i=>i.LeagueName==CurrentLeague.LeagueName).Divisions.ToList().Find(i=>i.DivisionName==CurrentDivision.DivisionName).
Teams.Add(new Team{TeamName=string.Format("New Team {0}",j++),Players=new ObservableCollection<Player>()});
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
}
}
public class League
{
public League()
{
}
public string LeagueName { get; set; }
public ObservableCollection<Division> Divisions { get; set; }
}
public class Division
{
public Division()
{
}
public string DivisionName { get; set; }
public ObservableCollection<Team> Teams { get; set; }
}
public class Team
{
public Team() { }
public string TeamName { get; set; }
public ObservableCollection<Player> Players { get; set; }
}
public class Player
{
public Player() { }
public string FName { get; set; }
public string LName { get; set; }
}
public class ListLeague : ObservableCollection<League>
{
public ListLeague()
{
League l;
Division d;
Division d1; Division d2; Division d3; Team t1; Team t2; Team t3; Team t4;
Player p1; Player p2; Player p3; Player p4;
Add(l = new League { LeagueName = "IPL" ,Divisions=new ObservableCollection<Division>()});
d = new Division { DivisionName = "EAST" ,Teams=new ObservableCollection<Team>()};
d1 = new Division { DivisionName = "WEST", Teams = new ObservableCollection<Team>() };
d2 = new Division { DivisionName = "SOUTH", Teams = new ObservableCollection<Team>() };
d3 = new Division { DivisionName = "NORTH", Teams = new ObservableCollection<Team>() };
l.Divisions.Add(d1);
l.Divisions.Add(d2);
l.Divisions.Add(d3);
l.Divisions.Add(d);
t1 = new Team { TeamName = "MI",Players=new ObservableCollection<Player>() };
t2 = new Team { TeamName = "KKR", Players = new ObservableCollection<Player>() };
t3 = new Team { TeamName = "RR", Players = new ObservableCollection<Player>() };
t4 = new Team { TeamName = "CS", Players = new ObservableCollection<Player>() };
d.Teams.Add(t1);
d1.Teams.Add(t2);
d2.Teams.Add(t3);
d3.Teams.Add(t4);
p1 = new Player { FName = "P1", LName = "p1" };
p2 = new Player { FName = "P1", LName = "p1" };
p3 = new Player { FName = "P1", LName = "p1" };
p4 = new Player { FName = "P1", LName = "p1" };
t1.Players.Add(p1);
t2.Players.Add(p2);
t3.Players.Add(p3);
t4.Players.Add(p4);
Add(l = new League { LeagueName = "ICL", Divisions = new ObservableCollection<Division>() });
d = new Division { DivisionName = "EAST", Teams = new ObservableCollection<Team>() };
d1 = new Division { DivisionName = "WEST", Teams = new ObservableCollection<Team>() };
d2 = new Division { DivisionName = "SOUTH", Teams = new ObservableCollection<Team>() };
d3 = new Division { DivisionName = "NORTH", Teams = new ObservableCollection<Team>() };
l.Divisions.Add(d1);
l.Divisions.Add(d2);
l.Divisions.Add(d3);
l.Divisions.Add(d);
t1 = new Team { TeamName = "MI", Players = new ObservableCollection<Player>() };
t2 = new Team { TeamName = "KKR", Players = new ObservableCollection<Player>() };
t3 = new Team { TeamName = "RR", Players = new ObservableCollection<Player>() };
t4 = new Team { TeamName = "CS", Players = new ObservableCollection<Player>() };
d.Teams.Add(t1);
d1.Teams.Add(t2);
d2.Teams.Add(t3);
d3.Teams.Add(t4);
p1 = new Player { FName = "P1", LName = "p1" };
p2 = new Player { FName = "P1", LName = "p1" };
p3 = new Player { FName = "P1", LName = "p1" };
p4 = new Player { FName = "P1", LName = "p1" };
t1.Players.Add(p1);
t2.Players.Add(p2);
t3.Players.Add(p3);
t4.Players.Add(p4);
}
public League this[string name]
{
get
{
foreach (League l in this)
if (l.LeagueName == name)
return l;
return null;
}
}
}
}