■ ListCollectionView 클래스를 사용해 데이터를 탐색/정렬하고 필터를 설정하는 방법을 보여준다.
▶ Order.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
using System; using System.ComponentModel; namespace TestProject { /// <summary> /// 주문 /// </summary> public class Order : INotifyPropertyChanged { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 속성 변경시 - PropertyChanged /// <summary> /// 속성 변경시 /// </summary> public event PropertyChangedEventHandler PropertyChanged; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 고객 ID /// </summary> private int customerID; /// <summary> /// 처리일 /// </summary> private DateTime datefilled; /// <summary> /// 처리 구분 /// </summary> private string filled; /// <summary> /// 제품 ID /// </summary> private int productID; /// <summary> /// 제품명 /// </summary> private string productName; /// <summary> /// 주문 ID /// </summary> private int orderID; /// <summary> /// 주문일 /// </summary> private DateTime orderdate; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 주문 ID - OrderID /// <summary> /// 주문 ID /// </summary> public int OrderID { get { return this.orderID; } set { this.orderID = value; FirePropertyChangedEvent("OrderID"); } } #endregion #region 고객 ID - CustomerID /// <summary> /// 고객 ID /// </summary> public int CustomerID { get { return this.customerID; } set { this.customerID = value; FirePropertyChangedEvent("CustomerID"); } } #endregion #region 제품명 - ProductName /// <summary> /// 제품명 /// </summary> public string ProductName { get { return this.productName; } set { this.productName = value; FirePropertyChangedEvent("ProductName"); } } #endregion #region 제품 ID - ProductID /// <summary> /// 제품 ID /// </summary> public int ProductID { get { return this.productID; } set { this.productID = value; FirePropertyChangedEvent("ProductID"); } } #endregion #region 처리 구분 - Filled /// <summary> /// 처리 구분 /// </summary> public string Filled { get { return this.filled; } set { this.filled = value; FirePropertyChangedEvent("Filled"); } } #endregion #region 주문일 - OrderDate /// <summary> /// 주문일 /// </summary> public DateTime OrderDate { get { return this.orderdate; } set { this.orderdate = value; FirePropertyChangedEvent("OrderDate"); } } #endregion #region 처리일 - DateFilled /// <summary> /// 처리일 /// </summary> public DateTime DateFilled { get { return this.datefilled; } set { this.datefilled = value; FirePropertyChangedEvent("DateFilled"); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Order(orderID, customerID, productName, productID, filled, orderDate) /// <summary> /// 생성자 /// </summary> /// <param name="orderID">주문 ID</param> /// <param name="customerID">고객 ID</param> /// <param name="productName">제품명</param> /// <param name="productID">제품 ID</param> /// <param name="filled">처리 구분</param> /// <param name="orderDate">주문일</param> public Order(int orderID, int customerID, string productName, int productID, string filled, DateTime orderDate) { OrderID = orderID; CustomerID = customerID; ProductName = productName; ProductID = productID; Filled = filled; OrderDate = orderDate; } #endregion #region 생성자 - Order(orderID, customerID, productName, productID, filled, orderDate, dateFilled) /// <summary> /// 생성자 /// </summary> /// <param name="orderID">주문 ID</param> /// <param name="customerID">고객 ID</param> /// <param name="productName">제품명</param> /// <param name="productID">제품 ID</param> /// <param name="filled">처리 구분</param> /// <param name="orderDate">주문일</param> /// <param name="dateFilled">처리일</param> public Order(int orderID, int customerID, string productName, int productID, string filled, DateTime orderDate, DateTime dateFilled) { OrderID = orderID; CustomerID = customerID; ProductName = productName; ProductID = productID; Filled = filled; OrderDate = orderDate; DateFilled = dateFilled; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 속성 변경시 이벤트 발생시키기 - FirePropertyChangedEvent(propertyName) /// <summary> /// 속성 변경시 이벤트 발생시키기 /// </summary> /// <param name="propertyName">속성명</param> private void FirePropertyChangedEvent(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #endregion } } |
▶ OrderCollection.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
using System; using System.Collections.ObjectModel; namespace TestProject { /// <summary> /// 주문 컬렉션 /// </summary> public class OrderCollection : ObservableCollection<Order> { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 주문 1 /// </summary> public Order order1 = new Order ( 1001, 5682, "Blue Sweater", 44, "Yes", new DateTime(2003, 1, 23), new DateTime(2003, 2, 4) ); /// <summary> /// 주문 2 /// </summary> public Order order2 = new Order ( 1002, 2211, "Gray Jacket Long", 181, "No", new DateTime(2003, 2, 14) ); /// <summary> /// 주문 3 /// </summary> public Order order3 = new Order ( 1003, 5682, "Brown Pant W", 2, "Yes", new DateTime(2002, 12, 20), new DateTime(2003, 1, 13) ); /// <summary> /// 주문 4 /// </summary> public Order order4 = new Order ( 1004, 3143, "Orange T-shirt", 205, "No", new DateTime(2003, 1, 28) ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - OrderCollection() /// <summary> /// 생성자 /// </summary> public OrderCollection() { Add(order1); Add(order2); Add(order3); Add(order4); } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
<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" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <local:OrderCollection x:Key="OrderCollectionKey" /> <DataTemplate x:Key="OrderCellTemplateKey"> <Grid Width="600" Height="20"> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <TextBlock Grid.Column="0" Text="{Binding Path=OrderID}" /> <TextBlock Grid.Column="1" Text="{Binding Path=CustomerID}" /> <TextBlock Grid.Column="2" Text="{Binding Path=ProductName}" /> <TextBlock Grid.Column="3" Text="{Binding Path=ProductID}" /> <TextBlock Grid.Column="4" Text="{Binding Path=Filled}" /> </Grid> </DataTemplate> </Window.Resources> <DockPanel Name="dockPanel" Height="400"> <DockPanel.DataContext> <Binding Source="{StaticResource OrderCollectionKey}" /> </DockPanel.DataContext> <Grid DockPanel.Dock="Top" Width="600"> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> <RowDefinition Height="100" /> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Grid.ColumnSpan="5" Margin="0 0 0 10" FontWeight="Bold"> Click a column button to sort the list </TextBlock> <Button Name="orderIDHeaderButton" Grid.Row="1" Grid.Column="0" Content="Order ID" /> <Button Name="customerIDHeaderButton" Grid.Row="1" Grid.Column="1" Content="Customer ID" /> <Button Name="productNameHeaderButton" Grid.Row="1" Grid.Column="2" Content="Product Name" /> <Button Name="productIDHeaderButton" Grid.Row="1" Grid.Column="3" Content="Product ID" /> <Button Name="filledHeaderButton" Grid.Row="1" Grid.Column="4" Content="Filled?" /> <ListBox Grid.Row="2" Grid.ColumnSpan="5" IsSynchronizedWithCurrentItem="True" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemTemplate="{StaticResource OrderCellTemplateKey}" ItemsSource="{Binding Source={StaticResource OrderCollectionKey}}" /> </Grid> <Border Margin="0 10 0 0" Width="600" Height="200" CornerRadius="10" BorderBrush="RoyalBlue" BorderThickness="5"> <Grid HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition Height="25" /> <RowDefinition Height="35" /> <RowDefinition /> <RowDefinition /> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <TextBlock Name="feedbackTextBlock" Grid.Row="0" Foreground="Red" /> <TextBlock Grid.Row="1" Grid.ColumnSpan="4" Margin="0 0 0 10"> Click Next/Previous to Browse. Click Show to Filter </TextBlock> <Button Name="movePreviousItemButton" Grid.Row="2" Grid.Column="0" Margin="0 0 0 10"> Previous </Button> <Button Name="moveNextItemButton" Grid.Row="2" Grid.Column="1" Margin="0 0 0 10"> Next </Button> <Button Name="filterButton" Grid.Row="2" Grid.Column="2" Margin="0 0 0 10"> Show only Unfilled </Button> <Button Name="unfilterButton" Grid.Row="2" Grid.Column="3" Margin="0 0 0 10"> Show all </Button> <TextBlock Grid.Row="3" Grid.Column="0" HorizontalAlignment="Right"> Order Number : </TextBlock> <TextBlock Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="2" Margin="10 0 0 0" Text="{Binding Path=OrderID}" /> <TextBlock Grid.Row="4" Grid.Column="0" HorizontalAlignment="Right"> Customer ID : </TextBlock> <TextBlock Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2" Margin="10 0 0 0" Text="{Binding Path=CustomerID}" /> <TextBlock Grid.Row="5" Grid.Column="0" HorizontalAlignment="Right"> Order Date : </TextBlock> <TextBlock Grid.Row="5" Grid.Column="1" Grid.ColumnSpan="2" Margin="10 0 0 0" Text="{Binding Path=OrderDate}" /> <TextBlock Grid.Row="6" Grid.Column="0" HorizontalAlignment="Right"> Filled Date : </TextBlock> <TextBlock Grid.Row="6" Grid.Column="1" Grid.ColumnSpan="2" Margin="10 0 0 0" Text="{Binding Path=DateFilled}" /> </Grid> </Border> </DockPanel> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
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 ListCollectionView listCollectionView; /// <summary> /// 주문 /// </summary> private Order order; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.listCollectionView = (ListCollectionView)CollectionViewSource.GetDefaultView(this.dockPanel.DataContext); this.orderIDHeaderButton.Click += headerButton_Click; this.customerIDHeaderButton.Click += headerButton_Click; this.productNameHeaderButton.Click += headerButton_Click; this.productIDHeaderButton.Click += headerButton_Click; this.filledHeaderButton.Click += headerButton_Click; this.movePreviousItemButton.Click += moveItemButton_Click; this.moveNextItemButton.Click += moveItemButton_Click; this.filterButton.Click += filterButton_Click; this.unfilterButton.Click += filterButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 헤더 버튼 클릭시 처리하기 - headerButton_Click(sender, e) /// <summary> /// 헤더 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void headerButton_Click(object sender, RoutedEventArgs e) { Button button = sender as Button; this.listCollectionView.SortDescriptions.Clear(); switch(button.Name) { case "orderIDHeaderButton" : this.listCollectionView.SortDescriptions.Add(new SortDescription("OrderID", ListSortDirection.Ascending)); break; case "customerIDHeaderButton" : this.listCollectionView.SortDescriptions.Add(new SortDescription("CustomerID", ListSortDirection.Ascending)); break; case "productNameHeaderButton" : this.listCollectionView.SortDescriptions.Add(new SortDescription("ProductName", ListSortDirection.Ascending)); break; case "productIDHeaderButton" : this.listCollectionView.SortDescriptions.Add(new SortDescription("ProductID", ListSortDirection.Ascending)); break; case "filledHeaderButton" : this.listCollectionView.SortDescriptions.Add(new SortDescription("Filled", ListSortDirection.Ascending)); break; } } #endregion #region 항목 이동 버튼 클릭시 처리하기 - moveItemButton_Click(sender, e) /// <summary> /// 항목 이동 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void moveItemButton_Click(object sender, RoutedEventArgs e) { Button button = sender as Button; switch(button.Name) { case "movePreviousItemButton" : if(this.listCollectionView.MoveCurrentToPrevious()) { this.feedbackTextBlock.Text = string.Empty; this.order = this.listCollectionView.CurrentItem as Order; } else { this.listCollectionView.MoveCurrentToFirst(); this.feedbackTextBlock.Text = "At first record"; } break; case "moveNextItemButton" : if(this.listCollectionView.MoveCurrentToNext()) { this.feedbackTextBlock.Text = string.Empty; this.order = this.listCollectionView.CurrentItem as Order; } else { this.listCollectionView.MoveCurrentToLast(); this.feedbackTextBlock.Text = "At last record"; } break; } } #endregion #region 필터 버튼 클릭시 처리하기 - filterButton_Click(sender, e) /// <summary> /// 필터 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void filterButton_Click(object sender, RoutedEventArgs e) { Button button = sender as Button; switch(button.Name) { case "filterButton" : this.listCollectionView.Filter = Contains; break; case "unfilterButton" : this.listCollectionView.Filter = null; break; } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 포함 여부 구하기 - Contains(source) /// <summary> /// 포함 여부 구하기 /// </summary> /// <param name="source">소스 객체</param> /// <returns>포함 여부</returns> private bool Contains(object source) { Order order = source as Order; return order?.Filled == "No"; } #endregion } } |