KeiStory

WPF CollectionView 의 SortDescriptions 를 이용해 데이터 정렬하기

 

CollectionView 의 SortDescriptions 를 데이터 정렬 기능을 알아봅니다.

Person.cs

using System.ComponentModel;
 
namespace WpfApp
{
    public class Person : INotifyPropertyChanged
    {
        /// <summary>
        /// 속성변경 이벤트입니다.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;
 
        /// <summary>
        /// 이름입니다.
        /// </summary>
        string name = "";
 
        /// <summary>
        /// 별명입니다.
        /// </summary>
        string nickName = "";
 
        /// <summary>
        /// 이름입니다.
        /// </summary>
        public string Name
        {
            set
            {
                this.name = value;
                OnPropertyChanged(nameof(Name));
            }
            get { return name; }
        }
 
        /// <summary>
        /// 별명입니다.
        /// </summary>
        public string NickName
        {
            set
            {
                nickName = value;
                OnPropertyChanged(nameof(NickName));
            }
            get { return nickName; }
        }
 
        /// <summary>
        /// 속성 값이 변경될 때 발생합니다.
        /// </summary>
        /// <param name="propertyName"></param>
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

MainWindow.xaml

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <ListBox Name="listbox" Width="300" Height="300" Margin="24">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Name}" />
                        <TextBlock Text="      " />
                        <TextBlock Text="{Binding NickName}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
</Window>

MainWindow.xaml.cs

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
 
namespace WpfApp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
 
            ObservableCollection<Person> datas = new ObservableCollection<Person>();
 
            datas.Add(new Person(){ Name = "Kang",  NickName = "Super" });
            datas.Add(new Person(){ Name = "An",    NickName = "Father" });
            datas.Add(new Person(){ Name = "Jang",  NickName = "Marvel" });
 
            ICollectionView collectionView = CollectionViewSource.GetDefaultView(datas);
 			
            // 데이터 정렬
            collectionView.SortDescriptions.Add(new SortDescription(nameof(Person.Name), ListSortDirection.Ascending));
 
            this.listbox.ItemsSource = collectionView;
        }
    }
}

결과

 

반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band