첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ 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"
    xmlns:s="clr-namespace:System.Collections;assembly=mscorlib"
    xmlns:p="clr-namespace:System;assembly=mscorlib"
    Width="800"
    Height="600"
    Title="ListView 클래스 : 그리드 뷰 컬럼 헤더 클릭시 정렬하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid>
        <ListView Name="listView"
            HorizontalAlignment="Center" 
            VerticalAlignment="Center"
            Height="200"
            GridViewColumnHeader.Click="gridViewColumnHeader_Click">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="100" Header="Year"  DisplayMemberBinding="{Binding Path=Year}"  />
                    <GridViewColumn Width="100" Header="Month" DisplayMemberBinding="{Binding Path=Month}" />
                    <GridViewColumn Width="100" Header="Day"   DisplayMemberBinding="{Binding Path=Day}"   />
                </GridView>            
            </ListView.View>
            <ListView.ItemsSource>
                <s:ArrayList>
                    <p:DateTime>1993/1/1 12:22:02</p:DateTime>
                    <p:DateTime>1993/1/2 13:2:01</p:DateTime>
                    <p:DateTime>1997/1/3 2:1:6</p:DateTime>
                    <p:DateTime>1997/1/4 13:6:55</p:DateTime>
                    <p:DateTime>1999/2/1 12:22:02</p:DateTime>
                    <p:DateTime>1998/2/2 13:2:01</p:DateTime>
                    <p:DateTime>2000/2/3 2:1:6</p:DateTime>
                    <p:DateTime>2002/2/4 13:6:55</p:DateTime>
                    <p:DateTime>2001/3/1 12:22:02</p:DateTime>
                    <p:DateTime>2006/3/2 13:2:01</p:DateTime>
                    <p:DateTime>2004/3/3 2:1:6</p:DateTime>
                    <p:DateTime>2004/3/4 13:6:55</p:DateTime>
                </s:ArrayList>
            </ListView.ItemsSource>
        </ListView>
    </Grid>
</Window>

 

728x90

 

▶ MainWindow.xaml.cs

using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 마지막 클릭 그리드 뷰 컬럼 헤더
        /// </summary>
        private GridViewColumnHeader lastClickedGridViewColumnHeader = null;

        /// <summary>
        /// 마지막 리스트 정렬 방향
        /// </summary>
        private ListSortDirection lastListSortDirection = ListSortDirection.Ascending;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 그리드 뷰 컬럼 헤더 클릭시 처리하기 - gridViewColumnHeader_Click(sender, e)

        /// <summary>
        /// 그리드 뷰 컬럼 헤더 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void gridViewColumnHeader_Click(object sender, RoutedEventArgs e)
        {
            GridViewColumnHeader clickedGridViewColumnHeader = e.OriginalSource as GridViewColumnHeader;

            ListSortDirection listSortDirection;

            if(clickedGridViewColumnHeader != null)
            {
                if(clickedGridViewColumnHeader.Role != GridViewColumnHeaderRole.Padding)
                {
                    // 마지막으로 클릭한 그리브 뷰 컬럼 헤더가 아닌 경우
                    if(clickedGridViewColumnHeader != this.lastClickedGridViewColumnHeader)
                    {
                        listSortDirection = ListSortDirection.Ascending;
                    }
                    else // 마지막으로 클릭한 그리드 뷰 컬럼 헤더인 경우
                    {
                        if(this.lastListSortDirection == ListSortDirection.Ascending)
                        {
                            listSortDirection = ListSortDirection.Descending;
                        }
                        else
                        {
                            listSortDirection = ListSortDirection.Ascending;
                        }
                    }

                    string header = clickedGridViewColumnHeader.Column.Header as string;

                    Sort(header, listSortDirection);

                    this.lastClickedGridViewColumnHeader = clickedGridViewColumnHeader;
                    this.lastListSortDirection           = listSortDirection;
                }
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Function

        #region 정렬하기 - Sort(header, listSortDirection)

        /// <summary>
        /// 정렬하기
        /// </summary>
        /// <param name="header">헤더</param>
        /// <param name="listSortDirection">리스트 정렬 방향</param>
        private void Sort(string header, ListSortDirection listSortDirection)
        {
            ICollectionView collectionView = CollectionViewSource.GetDefaultView(this.listView.ItemsSource);

            collectionView.SortDescriptions.Clear();

            SortDescription sortDescription = new SortDescription(header, listSortDirection);

            collectionView.SortDescriptions.Add(sortDescription);

            collectionView.Refresh();
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요