huizhejian 2019-06-26
实现效果:
关键字:
代码:
xaml:
每层对应一个对象的列表
<DockPanel.Resources> <local:ListLeagueList x:Key="MyList"/> <HierarchicalDataTemplate DataType = "{x:Type local:League}" ItemsSource = "{Binding Path=Divisions}"> <TextBlock Text="{Binding Path=Name}"/> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType = "{x:Type local:Division}" ItemsSource = "{Binding Path=Teams}"> <TextBlock Text="{Binding Path=Name}"/> </HierarchicalDataTemplate> <DataTemplate DataType="{x:Type local:Team}"> <TextBlock Text="{Binding Path=Name}"/> </DataTemplate> </DockPanel.Resources>
绑定数据源的构成
public class ListLeagueList : List<League> { public ListLeagueList() { League l; Division d; Add(l = new League("League A")); l.Divisions.Add((d = new Division("Division A"))); d.Teams.Add(new Team("Team I")); d.Teams.Add(new Team("Team II")); d.Teams.Add(new Team("Team III")); d.Teams.Add(new Team("Team IV")); d.Teams.Add(new Team("Team V")); l.Divisions.Add((d = new Division("Division B"))); d.Teams.Add(new Team("Team Blue"));
扩展:
绑定后若需动态添加或删除子项,建议继承ObservableCollection<T>
继承的话一般有:
public League this[string name] => this.FirstOrDefault(l => l.Name == name);