첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ CollectionViewSource 엘리먼트 : 계층적 데이터 바인딩 하기

------------------------------------------------------------------------------------------------------------------------

Team.cs

 

 

/// <summary>

///

/// </summary>

public class Team

{

    //////////////////////////////////////////////////////////////////////////////////////////////////// Property

    ////////////////////////////////////////////////////////////////////////////////////////// Public

 

    #region 명칭 - Name

 

    /// <summary>

    /// 명칭

    /// </summary>

    public string Name { get; set; }

 

    #endregion

}

 

 

Division.cs

 

 

using System;

using System.Collections.ObjectModel;

 

/// <summary>

/// 부문

/// </summary>

public class Division

{

    //////////////////////////////////////////////////////////////////////////////////////////////////// Property

    ////////////////////////////////////////////////////////////////////////////////////////// Public

 

    #region 명칭 - Name

 

    /// <summary>

    /// 명칭

    /// </summary>

    public string Name { get; set; }

 

    #endregion

 

    #region 팀 컬렉션 - TeamCollection

 

    /// <summary>

    /// 팀 컬렉션

    /// </summary>

    public ObservableCollection<Team> TeamCollection { get; set; }

 

    #endregion

}

 

 

League.cs

 

 

using System;

using System.Collections.ObjectModel;

 

/// <summary>

/// 리그

/// </summary>

public class League

{

    //////////////////////////////////////////////////////////////////////////////////////////////////// Property

    ////////////////////////////////////////////////////////////////////////////////////////// Public

 

    #region 명칭 - Name

 

    /// <summary>

    /// 명칭

    /// </summary>

    public string Name { get; set; }

 

    #endregion

 

    #region 부문 컬렉션 - DivisionCollection

 

    /// <summary>

    /// 부문 컬렉션

    /// </summary>

    public ObservableCollection<Division> DivisionCollection { get; set; }

 

    #endregion

}

 

 

LeagueCollection.cs

 

 

using System;

using System.Collections.ObjectModel;

 

/// <summary>

/// 리그 컬렉션

/// </summary>

public class LeagueCollection : ObservableCollection<League>

{

    //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

    ////////////////////////////////////////////////////////////////////////////////////////// Public

 

    #region 생성자 - LeagueCollection()

 

    /// <summary>

    /// 생성자

    /// </summary>

    public LeagueCollection()

    {

        for(int x = 1; x < 3; x++)

        {

            League league = new League()

            {

                Name               = "League " + x,

                DivisionCollection = new ObservableCollection<Division>()

            };

 

            for(int y = 1; y < 4; y++)

            {

                Division division = new Division()

                {

                    Name           = string.Format("Division {0}-{1}", x, y),

                    TeamCollection = new ObservableCollection<Team>()

                };

 

                for(int z = 1; z < 5; z++)

                {

                    Team team = new Team()

                    {

                        Name = string.Format("Team {0}-{1}-{2}", x, y, z)

                    };

 

                    division.TeamCollection.Add(pTeam);

                }

 

                league.DivisionCollection.Add(pDivision);

            }

 

            Add(league);

        }

    }

 

    #endregion

}

 

 

MainPage.xaml

 

 

<UserControl x:Class="MasterDetailBindingApplication.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="clr-namespace:MasterDetailBindingApplication">

    <UserControl.Resources>

        <local:LeagueCollection x:Key="LeagueCollectionKey" />

        <CollectionViewSource x:Key="LeagueCollectionViewSourceKey"

            Source="{StaticResource LeagueCollectionKey}" />

        <CollectionViewSource x:Key="DivisionCollectionViewSourceKey"

            Source="{Binding DivisionCollection, Source={StaticResource LeagueCollectionViewSourceKey}}" />

        <CollectionViewSource x:Key="TeamCollectionViewSourceKey"

            Source="{Binding TeamCollection, Source={StaticResource DivisionCollectionViewSourceKey}}" />

    </UserControl.Resources>

    <StackPanel x:Name="rootStackPanel"

        DataContext="{Binding Source={StaticResource LeagueCollectionViewSourceKey}}"

        Margin="5"

        Orientation="Horizontal">

        <StackPanel Margin="5">

            <TextBlock

                Margin="3"

                FontWeight="Bold"

                Text="All Leagues" />

            <ListBox

                DisplayMemberPath="Name"

                ItemsSource="{Binding}" />

        </StackPanel>

        <StackPanel Margin="5">

            <TextBlock

                Margin="3"

                FontWeight="Bold"

                Text="{Binding Name}" />

            <ListBox

                DisplayMemberPath="Name"

                ItemsSource="{Binding Source={StaticResource DivisionCollectionViewSourceKey}}" />

        </StackPanel>

        <StackPanel

            DataContext="{Binding Source={StaticResource DivisionCollectionViewSourceKey}}"

            Margin="5">

            <TextBlock

                Margin="3"

                FontWeight="Bold"

                Text="{Binding Name}" />

            <ListBox

                DisplayMemberPath="Name"

                ItemsSource="{Binding Source={StaticResource TeamCollectionViewSourceKey}}" />

        </StackPanel>

    </StackPanel>

</UserControl>

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요