■ ListBox 클래스 : 그룹 사용하기

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


TestProject.zip


PeriodGroupDescription.cs

 

 

using System;

using System.ComponentModel;

using System.Globalization;

 

namespace TestProject

{

    /// <summary>

    /// 기간 그룹 설명자

    /// </summary>

    public class PeriodGroupDescription : GroupDescription

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

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

 

        #region 항목에서 그룹 명칭 구하기 - GroupNameFromItem(item, level, cultureInfo)

 

        /// <summary>

        /// 항목에서 그룹 명칭 구하기

        /// </summary>

        /// <param name="item">항목</param>

        /// <param name="level">레벨</param>

        /// <param name="cultureInfo">CultureInfo 객체</param>

        /// <returns>그룹 명칭</returns>

        public override object GroupNameFromItem(object item, int level, CultureInfo cultureInfo)

        {

            Person person = item as Person;

 

            if(person.BirthDate == null)

            {

                return "Unknown";

            }

 

            int year = ((DateTime)person.BirthDate).Year;

 

            if(year < 1575)

            {

                return "Pre-Baroque";

            }

 

            if(year < 1725)

            {

                return "Baroque";

            }

 

            if(year < 1795)

            {

                return "Classical";

            }

 

            if(year < 1870)

            {

                return "Romantic";

            }

 

            if(year < 1910)

            {

                return "20th Century";

            }

 

            return "Post-War";

        }

 

        #endregion

    }

}

 

 

MainWindow.xaml

 

 

<Window x:Class="TestProject.MainWindow"

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

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

    ResizeMode="CanMinimize"

    SizeToContent="WidthAndHeight"

    Title="ListBox 클래스 : 그룹 사용하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <DockPanel Name="dockPanel">

        <Menu DockPanel.Dock="Top">

            <MenuItem Header="_File">

                <MenuItem Header="_Open..." Command="Open" />

            </MenuItem>

        </Menu>

        <ListBox Name="listBox" Grid.Row="1"

            HorizontalAlignment="Center"

            VerticalAlignment="Center"

            Margin="24"

            Width="300"

            Height="300">

            <ListBox.ItemTemplate>

                <DataTemplate>

                    <StackPanel Orientation="Horizontal">

                        <TextBlock Text="{Binding Path=FirstName}" />

                        <TextBlock Text=" " />

                        <TextBlock Text="{Binding Path=MiddleName}" />

                        <TextBlock Name="spaceTextBlock" Text=" " />

                        <TextBlock Text="{Binding Path=LastName}" />

                        <TextBlock Text=" (" />

                        <TextBlock Text="{Binding Path=BirthDate.Year}" />

                        <TextBlock Text="-" />

                        <TextBlock Name="deathDateTextBlock" Text="{Binding Path=DeathDate.Year}" />

                        <TextBlock Text=")" />

                    </StackPanel>

                    <DataTemplate.Triggers>

                        <DataTrigger Binding="{Binding Path=DeathDate.Year}" Value="{x:Null}">

                            <Setter TargetName="deathDateTextBlock" Property="Text" Value="present" />

                        </DataTrigger>

                        <DataTrigger Binding="{Binding Path=MiddleName}" Value="">

                            <Setter TargetName="spaceTextBlock" Property="Text" Value="" />

                        </DataTrigger>

                    </DataTemplate.Triggers>

                </DataTemplate>

            </ListBox.ItemTemplate>

            <ListBox.GroupStyle>

                <GroupStyle>

                    <GroupStyle.HeaderTemplate>

                        <DataTemplate>

                            <TextBlock

                                Margin="6"

                                Foreground="White"

                                Background="DarkGray"

                                FontWeight="Bold"

                                FontSize="12pt"

                                Text="{Binding Path=Name}" />

                        </DataTemplate>

                    </GroupStyle.HeaderTemplate>

                </GroupStyle>

            </ListBox.GroupStyle>

        </ListBox>

    </DockPanel>

    <Window.CommandBindings>

        <CommandBinding Command="Open" Executed="ExecuteOpen" />

    </Window.CommandBindings>

</Window>

 

 

MainWindow.cs

 

 

using System;

using System.ComponentModel;

using System.Windows;

using System.Windows.Data;

using System.Windows.Input;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 리스트 컬렉션 뷰

        /// </summary>

        private ListCollectionView listCollectionView;

 

        #endregion

 

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

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

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

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

 

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        [STAThread]

        public static void Main()

        {

            Application application = new Application();

 

            application.Run(new MainWindow());

        }

 

        #endregion

 

        ////////////////////////////////////////////////////////////////////////////////////////// Instance

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region Open 실행하기 - ExecuteOpen(sender, e)

 

        /// <summary>

        /// Open 실행하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void ExecuteOpen(object sender, ExecutedRoutedEventArgs e)

        {

            People people = People.Load(this);

 

            if(people != null)

            {

                this.listCollectionView = new ListCollectionView(people);

 

                this.listCollectionView.SortDescriptions.Add(new SortDescription("BirthDate", ListSortDirection.Ascending));

 

                this.listCollectionView.GroupDescriptions.Add(new PeriodGroupDescription());

 

                this.listBox.ItemsSource = this.listCollectionView;

 

                if(this.listBox.Items.Count > 0)

                {

                    this.listBox.SelectedIndex = 0;

                }

            }

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요