{"id":37757,"date":"2024-11-01T10:00:11","date_gmt":"2024-11-01T10:00:11","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=37757"},"modified":"2024-11-01T11:03:51","modified_gmt":"2024-11-01T11:03:51","slug":"wpf-course-data-sorting-and-filtering-with-collectionview","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/37757\/","title":{"rendered":"WPF Course, Data Sorting and Filtering with CollectionView"},"content":{"rendered":"<p>Windows Presentation Foundation (WPF) is Microsoft&#8217;s UI framework widely used for developing desktop applications. WPF provides various data binding techniques. In this chapter, we will take a closer look at one of the key concepts in WPF, <strong>CollectionView<\/strong>, and explain how to sort and filter data.<\/p>\n<h2>1. What is CollectionView?<\/h2>\n<p>A CollectionView is an object used with WPF&#8217;s data binding functionality that provides a view of a data source. It manages the top-level collection and supports functionalities such as sorting, filtering, and grouping the contents of this collection. This allows the logical implementation of WPF&#8217;s MVVM pattern and enables more flexible handling of data in the UI.<\/p>\n<h2>2. Key Features of CollectionView<\/h2>\n<ul>\n<li><strong>Sorting<\/strong>: A CollectionView can sort items in the collection based on specific properties.<\/li>\n<li><strong>Filtering<\/strong>: You can select only the items that meet certain conditions to display in the collection.<\/li>\n<li><strong>Grouping<\/strong>: Items can be grouped based on specific properties of the data.<\/li>\n<\/ul>\n<h2>3. Creating a CollectionView<\/h2>\n<p>Creating a CollectionView is very simple. First, define the data to be used with the CollectionView, and then create the CollectionView using <strong>CollectionViewSource<\/strong>. Below is a simple example.<\/p>\n<pre><code class=\"language-csharp\">\nusing System.Collections.Generic;\nusing System.Windows;\nusing System.Windows.Data;\n\nnamespace WpfApp\n{\n    public partial class MainWindow : Window\n    {\n        public List&lt;Person&gt; People { get; set; }\n\n        public MainWindow()\n        {\n            InitializeComponent();\n            People = new List&lt;Person&gt;()\n            {\n                new Person { Name = \"John\", Age = 30 },\n                new Person { Name = \"Jane\", Age = 25 },\n                new Person { Name = \"Mike\", Age = 35 },\n            };\n\n            CollectionViewSource collectionViewSource = new CollectionViewSource();\n            collectionViewSource.Source = People;\n            this.DataContext = collectionViewSource.View;\n        }\n    }\n\n    public class Person\n    {\n        public string Name { get; set; }\n        public int Age { get; set; }\n    }\n}\n<\/code><\/pre>\n<p>In the example above, we created a <lt;Person&gt; class, and added instances with properties for name and age to a list. A CollectionView was created based on this list using <strong>CollectionViewSource<\/strong>.<\/p>\n<h2>4. Sorting Data<\/h2>\n<p>To sort data in a CollectionView, you must use the <strong>SortDescriptions<\/strong> property. Below is an example of sorting by age.<\/p>\n<pre><code class=\"language-csharp\">\ncollectionViewSource.SortDescriptions.Add(new SortDescription(\"Age\", ListSortDirection.Ascending);\n<\/code><\/pre>\n<p>This code sorts in ascending order based on the <strong>Age<\/strong> property. If you want to sort in descending order, use <strong>ListSortDirection.Descending<\/strong>.<\/p>\n<h2>5. Filtering Data<\/h2>\n<p>Filtering allows you to show only the items that meet specific conditions. To filter in the CollectionView, you need to define the <strong>Filter<\/strong> property. Below is an example of filtering to show only people aged 30 and over.<\/p>\n<pre><code class=\"language-csharp\">\ncollectionViewSource.Filter += (s, e) =&gt; \n{\n    if (e.Item is Person person)\n    {\n        e.Accepted = person.Age &gt;= 30;\n    }\n};\n<\/code><\/pre>\n<h2>6. Binding to the UI<\/h2>\n<p>After sorting or filtering the data, you can bind it to the UI for display. Below is how to set up the binding in XAML.<\/p>\n<pre><code class=\"language-xaml\">\n<listview itemssource=\"{Binding}\">\n    <listview.view>\n        <gridview>\n            <gridviewcolumn displaymemberbinding=\"{Binding Name}\" header=\"Name\"><\/gridviewcolumn>\n            <gridviewcolumn displaymemberbinding=\"{Binding Age}\" header=\"Age\"><\/gridviewcolumn>\n        <\/gridview>\n    <\/listview.view>\n<\/listview>\n<\/code><\/pre>\n<h2>7. Conclusion<\/h2>\n<p>Utilizing CollectionView in WPF allows for flexible sorting, filtering, and grouping of data sources. This plays a very important role in the implementation of the MVVM pattern. When developing applications using WPF, effectively employing these techniques can lead to more elegant and functional UIs.<\/p>\n<h3>8. References<\/h3>\n<ul>\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/framework\/wpf\/data\/collectionviewsource-overview\">MSDN: CollectionViewSource Overview<\/a><\/li>\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/framework\/wpf\/data\/how-to-filter-a-collectionview\">MSDN: How to: Filter a CollectionView<\/a><\/li>\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/framework\/wpf\/data\/how-to-sort-a-collectionview\">MSDN: How to: Sort a CollectionView<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Windows Presentation Foundation (WPF) is Microsoft&#8217;s UI framework widely used for developing desktop applications. WPF provides various data binding techniques. In this chapter, we will take a closer look at one of the key concepts in WPF, CollectionView, and explain how to sort and filter data. 1. What is CollectionView? A CollectionView is an object &hellip; <a href=\"https:\/\/atmokpo.com\/w\/37757\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;WPF Course, Data Sorting and Filtering with CollectionView&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[117],"tags":[],"class_list":["post-37757","post","type-post","status-publish","format-standard","hentry","category-wpf-programming"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>WPF Course, Data Sorting and Filtering with CollectionView - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/37757\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"WPF Course, Data Sorting and Filtering with CollectionView - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Windows Presentation Foundation (WPF) is Microsoft&#8217;s UI framework widely used for developing desktop applications. WPF provides various data binding techniques. In this chapter, we will take a closer look at one of the key concepts in WPF, CollectionView, and explain how to sort and filter data. 1. What is CollectionView? A CollectionView is an object &hellip; \ub354 \ubcf4\uae30 &quot;WPF Course, Data Sorting and Filtering with CollectionView&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/37757\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T10:00:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:03:51+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"1\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/37757\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37757\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"WPF Course, Data Sorting and Filtering with CollectionView\",\"datePublished\":\"2024-11-01T10:00:11+00:00\",\"dateModified\":\"2024-11-01T11:03:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37757\/\"},\"wordCount\":200,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"WPF Programming\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/37757\/\",\"url\":\"https:\/\/atmokpo.com\/w\/37757\/\",\"name\":\"WPF Course, Data Sorting and Filtering with CollectionView - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T10:00:11+00:00\",\"dateModified\":\"2024-11-01T11:03:51+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37757\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/37757\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/37757\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"WPF Course, Data Sorting and Filtering with CollectionView\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"WPF Course, Data Sorting and Filtering with CollectionView - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/37757\/","og_locale":"ko_KR","og_type":"article","og_title":"WPF Course, Data Sorting and Filtering with CollectionView - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Windows Presentation Foundation (WPF) is Microsoft&#8217;s UI framework widely used for developing desktop applications. WPF provides various data binding techniques. In this chapter, we will take a closer look at one of the key concepts in WPF, CollectionView, and explain how to sort and filter data. 1. What is CollectionView? A CollectionView is an object &hellip; \ub354 \ubcf4\uae30 \"WPF Course, Data Sorting and Filtering with CollectionView\"","og_url":"https:\/\/atmokpo.com\/w\/37757\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T10:00:11+00:00","article_modified_time":"2024-11-01T11:03:51+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"1\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/37757\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/37757\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"WPF Course, Data Sorting and Filtering with CollectionView","datePublished":"2024-11-01T10:00:11+00:00","dateModified":"2024-11-01T11:03:51+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/37757\/"},"wordCount":200,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["WPF Programming"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/37757\/","url":"https:\/\/atmokpo.com\/w\/37757\/","name":"WPF Course, Data Sorting and Filtering with CollectionView - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T10:00:11+00:00","dateModified":"2024-11-01T11:03:51+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/37757\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/37757\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/37757\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"WPF Course, Data Sorting and Filtering with CollectionView"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=37757"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37757\/revisions"}],"predecessor-version":[{"id":37758,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37757\/revisions\/37758"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=37757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=37757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=37757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}