CollectionView and CollectionViewSource in WPF

Source: Internet
Author: User

1,collectionview What is it?
In fact, when you bind a background data list to a list control, WPF adds a layer called CollectionView (List view) between the data list and the list control silently, and it supports many advanced operations, such as sorting, grouping, Filter etc. so that we can divide the process into 3 parts: the data list (which maintains the background data), the list view (which maintains some additional states, such as "Current item", "sort", etc.), and the list control (which is responsible for rendering the CollectionView, not the collection)
CollectionView is for data, but it does not change the data, only the "image" of the data "permutation" and so on. A set of data can have several CollectionView, It's like being able to shoot a few DV on a person. So for the background of a set of data we can provide users with a number of ways to display.
A list control, such as a ListBox, can be used for data, or for CollectionView, for example, we can manually add data to its Items collection, or you can use ItemsSource to specify the data source for the data itself, This requires us to manually maintain the relationship between the data and the controls. The latter is for CollectionView, and if you do not specify a CollectionView for the list control, then WPF will automatically insert a. (Note that it is confusing to specify both items and ItemsSource, so you cannot manipulate them at the same time)
For example: <listbox itemssource= "{Binding source={staticresource MyList}}"/>
Or:

CollectionView CV = new CollectionView (myList);
This.listBox.ItemsSource = CV;

What is 2,collectionviewsource?
CollectionViewSource is a XAML proxy for CollectionView, meaning that CollectionView cannot be used in XAML, and if you want to bind CollectionView to a list control in XML, then please use CollectionViewSource. Its basic relationship with CollectionView is "has A". CollectionViewSource has a view property of type CollectionView to specify its corresponding CollectionView object, corresponding to it, and a source property to indicate the source of the data. A simple process is: Bind the data list to the Source property of the CollectionViewSource, The ItemsSource property of the list control is then bound to the View property of CollectionViewSource. Why don't you just bind the list control's Itemsource property to the data list? It depends on whether you need to find the CollectionViewSource and then find the view to perform the views (such as sorting, navigating, etc.). This may be a bit confusing. Take a look at the following example:

<window.resources>        <XmlDataProviderx:key= "Employees"XPath= "/employees/*">            <x:XData>                <Employeesxmlns="">                    <EmployeeName= "Terry Adams"Type= "FTE"EmployeeNumber= "1" />                    <EmployeeName= "Claire O&apos;donnell"Type= "FTE"EmployeeNumber= "12345" />                    <EmployeeName= "Palle Peterson"Type= "FTE"EmployeeNumber= "5678" />                    <EmployeeName= "Amy E. Alberts"Type= "CSG"EmployeeNumber= "99222" />                    <EmployeeName= "Stefan Hesse"Type= "Vendor"EmployeeNumber="-" />                </Employees>            </x:XData>        </XmlDataProvider>        <DataTemplateDataType= "Employee">            <TextBlockText="{Binding [email protected]}" />        </DataTemplate>            </window.resources>    <StackPanel>        <ListBoxItemsSource="{Binding Source={staticresource Employees}}"/>    </StackPanel>

In the example above, we used the traditional way to bind the ListBox's ItemsSource to a xmldataprovider and worked well, and later we found that WPF could use CollectionView to sort the list, Of course this sort we want to just be in the presentation layer, so we decided to do it in XAML. When we actually revamp this code, we break our brains, because it's not easy to find CollectionView objects in XAML for our data.
In fact, we just need to change the process of data binding. We associate the data with the CollectionViewSource and then collectionviewsource the list control. Then we can insert any sort of order we want in CollectionViewSource.

<window.resources>        <XmlDataProviderx:key= "Employees"XPath= "/employees/*">            <x:XData>                <Employeesxmlns="">                    <EmployeeName= "Terry Adams"Type= "FTE"EmployeeNumber= "1" />                    <EmployeeName= "Claire O&apos;donnell"Type= "FTE"EmployeeNumber= "12345" />                    <EmployeeName= "Palle Peterson"Type= "FTE"EmployeeNumber= "5678" />                    <EmployeeName= "Amy E. Alberts"Type= "CSG"EmployeeNumber= "99222" />                    <EmployeeName= "Stefan Hesse"Type= "Vendor"EmployeeNumber="-" />                </Employees>            </x:XData>        </XmlDataProvider>                <CollectionViewSourcex:key= "CVS"Source="{Binding Source={staticresource Employees}, xpath=/employees/*}">            <collectionviewsource.sortdescriptions>               <!--Insert the sort description here -            </collectionviewsource.sortdescriptions>            <collectionviewsource.groupdescriptions>                <!--Insert a grouping description here -            </collectionviewsource.groupdescriptions>        </CollectionViewSource>                <DataTemplateDataType= "Employee">            <TextBlockText="{Binding [email protected]}" />        </DataTemplate>            </window.resources>    <StackPanel>        <ListBoxItemsSource="{Binding source={staticresource cvs}}"x:name= "LB"/>    </StackPanel>

CollectionView and CollectionViewSource in WPF

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.