[C#/WPF] CollectionViewSource 엘리먼트 : SortDescriptions/GroupDescriptions 속성을 사용해 데이터 정렬 및 그룹 설정하기
C#/WPF 2023. 2. 28. 20:28728x90
반응형
728x170
■ CollectionViewSource 엘리먼트의 SortDescriptions/GroupDescriptions 속성을 사용해 데이터를 정렬하고 그룹을 설정하는 방법을 보여준다.
▶ Place.cs
namespace TestProject
{
/// <summary>
/// 장소
/// </summary>
public class Place
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 도시명 - CityName
/// <summary>
/// 도시명
/// </summary>
public string CityName { get; set; }
#endregion
#region 주 - State
/// <summary>
/// 주
/// </summary>
public string State { get; set; }
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - Place()
/// <summary>
/// 생성자
/// </summary>
public Place()
{
CityName = string.Empty;
State = string.Empty;
}
#endregion
#region 생성자 - Place(cityName, state)
/// <summary>
/// 생성자
/// </summary>
/// <param name="cityName">도시명</param>
/// <param name="state">주</param>
public Place(string cityName, string state)
{
CityName = cityName;
State = state;
}
#endregion
}
}
▶ PlaceCollection.cs
using System.Collections.ObjectModel;
namespace TestProject
{
/// <summary>
/// 장소 컬렉션
/// </summary>
public class PlaceCollection : ObservableCollection<Place>
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - PlaceCollection()
/// <summary>
/// 생성자
/// </summary>
public PlaceCollection()
{
Add(new Place("Seattle" , "WA"));
Add(new Place("Redmond" , "WA"));
Add(new Place("Bellevue" , "WA"));
Add(new Place("Kirkland" , "WA"));
Add(new Place("Portland" , "OR"));
Add(new Place("San Francisco", "CA"));
Add(new Place("Los Angeles" , "CA"));
Add(new Place("San Diego" , "CA"));
Add(new Place("San Jose" , "CA"));
Add(new Place("Santa Ana" , "CA"));
Add(new Place("Bellingham" , "WA"));
}
#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"
xmlns:local="clr-namespace:TestProject"
xmlns:componentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
Width="800"
Height="600"
Title="TestProject"
FontFamily="나눔고딕코딩"
FontSize="16">
<Window.Resources>
<local:PlaceCollection x:Key="PlaceCollectionKey" />
<CollectionViewSource x:Key="PlaceCollectionViewSourceKey" Source="{StaticResource PlaceCollectionKey}">
<CollectionViewSource.SortDescriptions>
<componentModel:SortDescription PropertyName="CityName" />
</CollectionViewSource.SortDescriptions>
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="State" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<XmlDataProvider x:Key="XmlDataProviderKey"
XPath="Tasks/Task">
<x:XData>
<Tasks xmlns="">
<Task Name="Groceries"
Priority="2"
Type="Home">
<Description>Pick up Groceries and Detergent</Description>
</Task>
<Task Name="Laundry"
Priority="2"
Type="Home">
<Description>Do Laundry</Description>
</Task>
<Task Name="Email"
Priority="1"
Type="Work">
<Description>Email Clients</Description>
</Task>
<Task Name="Clean"
Priority="3"
Type="Work">
<Description>Clean my office</Description>
</Task>
<Task Name="Dinner"
Priority="1"
Type="Home">
<Description>Get ready for family reunion</Description>
</Task>
<Task Name="Proposals"
Priority="2"
Type="Work">
<Description>Review new budget proposals</Description>
</Task>
</Tasks>
</x:XData>
</XmlDataProvider>
<CollectionViewSource x:Key="TaskCollectionViewSourceKey"
Source="{StaticResource XmlDataProviderKey}">
<CollectionViewSource.SortDescriptions>
<componentModel:SortDescription PropertyName="@Priority" />
</CollectionViewSource.SortDescriptions>
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="@Priority" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Window.Resources>
<DockPanel Margin="10">
<ListBox
Width="200"
Padding="5"
DisplayMemberPath="CityName"
ItemsSource="{Binding Source={StaticResource PlaceCollectionViewSourceKey}}">
<ListBox.GroupStyle>
<x:Static Member="GroupStyle.Default" />
</ListBox.GroupStyle>
</ListBox>
<ListBox
Margin="10 0 0 0"
Padding="5"
ItemsSource="{Binding Source={StaticResource TaskCollectionViewSourceKey}}" />
</DockPanel>
</Window>
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] 마스터/상세 XML 데이터 바인딩하기 (0) | 2023.03.02 |
---|---|
[C#/WPF] 마스터/상세 데이터 바인딩하기 (0) | 2023.03.01 |
[C#/WPF] ListCollectionView 클래스 : 데이터 탐색/정렬 및 필터 설정하기 (0) | 2023.02.28 |
[C#/WPF] MultiBinding 엘리먼트 : Converter 속성에서 IMultiValueConverter 인터페이스 구현 객체를 사용해 바인딩하기 (0) | 2023.02.28 |
[C#/WPF] XmlDataProvider 엘리먼트 : XPath 속성을 사용해 XML 데이터 사용하기 (0) | 2023.02.28 |
[C#/WPF] CompositeCollection 엘리먼트 : 다수 컬렉션과 항목을 하나의 목록으로 만들기 (0) | 2023.02.25 |
[C#/WPF] IDataErrorInfo 인터페이스 : 커스텀 객체에 대한 검증 로직 구현하기 (0) | 2023.02.24 |
[C#/WPF] 바인딩 유효성 검사 구현하기 (0) | 2023.02.24 |
[C#/WPF] Binding 엘리먼트 : Source/Path 속성을 사용해 ObjectDataProvider 객체 메소드 바인딩하기 (0) | 2023.02.23 |
[C#/WPF] Binding 엘리먼트 : ElementName/Path 속성을 사용해 두 컨트롤 속성 바인딩하기 (0) | 2023.02.23 |
댓글을 달아 주세요