WPF: Copy icollectionview to keep the memory after datasource changes

Source: Internet
Author: User

After you change the datasource of the itemscontrol of WPF, the previous icollectionview will disappear, so the data sorting, classification, and handsome selection function will disappear, because the data is kept in the icollectionview. However, The ListBox or listview of WPF can select the object selected in the previous datasource.

 

The followingProgramAfter you rebind the data, if the "Save status" is selected, the listview retains the status of the previous data binding. At the same time, the last equivalent Project (if any) will be selected. This is done through the equals method of the data object. However, this function has mentioned that even if you do not specifically copy icollectionview, The ListBox/listview of WPF will do the same. (Of course, the program is rebound.CodeA project that is the same as the selected project is automatically generated)

 

The key method is:

Static IcollectionviewCopy (IcollectionviewView,ObjectOBJ)

{

VaRRET= Collectionviewsource.Getdefaview view (OBJ );

Foreach(VaRGroupInView.Groupdescriptions)

RET.Groupdescriptions.Add (group );

Foreach(VaRSortInView.Sortdescriptions)

RET.Sortdescriptions.Add (SORT );

 

RET.Filter=View.Filter;

RET.Movecurrentto (View.Currentitem );

ReturnRET;

}

 

Source Code download

note: this is an archive of Microsoft SkyDrive. Please download it directly in a browser. Some download tools may not be available for download.

source code environment: Microsoft Visual C #2010 express

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.