階層式顯示

  • 1107
  • 0

摘要:階層式顯示

這個範例是從MSDN上抄下來的,我再加上第四層驗證自已的想法是否正確。

假設我們有一個LeagueList,包含多個League,每個League有一個包含多個Division的Divisions屬性,每個Division有一個包含多個Team的Teams屬性,每一個Team有一個包含多個Person的Persons屬性,然後我們用四個ListBox來顯示它們的階層關係。

XAML中,ListBox的ItemsSource做Binding的重點在於用"/"來取得下一層物件的屬性。並且注意要設定IsSynchronizedWithCurrentItem=true,子項才會跟著user選擇而變。

以下是c#的CODE:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace WpfApplication2
{
    public class Person
    {
        public string Name { get; set; }
        public Person(string name)
        {
            Name = name;
        }
    }

    public class Team
    {
        public string Name { get; set; }
        ObservableCollection persons;
        public ObservableCollection Persons
        {
            get { return persons; }
            set { persons = value; }
        }
        public Team(string teamname)
        {
            Name = teamname;
            persons = new ObservableCollection();
            persons.Add(new Person(Name + "_" + "Alvin"));
            persons.Add(new Person(Name + "_" + "Tony"));
            persons.Add(new Person(Name + "_" + "Andy"));
            persons.Add(new Person(Name + "_" + "Michelle"));
        }
        
    }

    public class Division
    {
        public string Name { get; set; }
        ObservableCollection teams;
        public ObservableCollection Teams
        {
            get { return teams; }
            set { teams = value; }
        }

        public Division(string name)
        {
            Name = name;
            teams = new ObservableCollection();
            teams.Add(new Team(Name + "_" + "Team Taipei"));
            teams.Add(new Team(Name + "_" + "Team Hsing Chu"));
            teams.Add(new Team(Name + "_" + "Team Tao ean"));
        }
    }

    public class League
    {
        public string Name { get; set; }
        ObservableCollection divisions;
        public ObservableCollection Divisions
        {
            get { return divisions; }
            set { divisions = value; }
        }

        public League(string name)
        {
            Name = name;
            divisions = new ObservableCollection();
            divisions.Add(new Division(Name + "_" + "Division A"));
            divisions.Add(new Division(Name + "_" + "Division B"));
            divisions.Add(new Division(Name + "_" + "Division C"));
        }
    }

    public class LeagueList:ObservableCollection
    {
        public LeagueList()
        {
            Add(new League("League A"));
            Add(new League("League B"));
            Add(new League("League C"));
        }
    }

}

以下是XAML的CODE:


<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:src="clr-namespace:WpfApplication2"
        Title="Master-Detail Binding" Background="Silver"
        >
    <Window.Resources>
        <src:LeagueList x:Key="MyList"/>
    </Window.Resources>
    <DockPanel DataContext="{Binding Source={StaticResource MyList}}">
        <Label DockPanel.Dock="Top" Content="Master-Detail Binding Scenario" FontWeight="Black" FontSize="24"/>
        <StackPanel>
            <Label>My Soccer Leagues</Label>
            <ListBox ItemsSource="{Binding}" DisplayMemberPath="Name" IsSynchronizedWithCurrentItem="True"/>            
        </StackPanel>
        <StackPanel>
            <Label Content="{Binding Path=Name}"/>
            <ListBox ItemsSource="{Binding Path=Divisions}" DisplayMemberPath="Name" IsSynchronizedWithCurrentItem="True"/>            
        </StackPanel>
        <StackPanel>
            <Label Content="{Binding Path=Divisions/Name}"/>
            <ListBox ItemsSource="{Binding Path=Divisions/Teams}" DisplayMemberPath="Name" IsSynchronizedWithCurrentItem="True"/>
        </StackPanel>
        <StackPanel>
            <Label Content="{Binding Path=Divisions/Teams/Name}"/>
            <ListBox ItemsSource="{Binding Path=Divisions/Teams/Persons}" DisplayMemberPath="Name"/>
        </StackPanel>
    </DockPanel>
</Window>