WPF: automatically slide The ListBox/listview scroll bar along with the data through additional properties

Source: Internet
Author: User

This requirement is very simple. When new data in ListBox or listview is added, the scroll bar of ListBox/listview will automatically decline. In fact, the additional attributes can be applied to any itemscontrol, the control template of itemscontrol must have the scrollviewer control definition.

(For example, this list is common in setup programs and is used to display installed files .)

 

Manual implementation of this requirement is also very simple, but the reuse of additional property code will be higher. You only need to do this:

<! -- LOC is the local namespace of listscroll -->

<ListBox LOC: listscroll. isenabled = "true" name = "list1"/>

 

Then, no matter whether the itemssource Of The ListBox changes data or you directly add the listboxitem through the items attribute, the scroll bar of The ListBox will automatically slide, as demonstrated by the example program:

 

There are two things to explain in specific implementation.

The first is the awareness of itemscontrol data changes. Note that you must consider changing the bound data source and directly modifying the itemscontrol subnode. However, the good news is that the items attribute of itemscontrol can be used in both cases.

The items attribute type of itemscontrol is itemcollection, And the itemcollection type is also collectionview. Therefore, it also provides collection change notifications (inheriting the system. Collections. Specialized. inotifycollectionchanged interface ).

 

You can use the collectionchanged event of the inotifycollectionchanged interface to detect data changes.

Note that itemcollection displays the collectionchanged event that executes the inotifycollectionchanged interface. Therefore, type conversion is required to use this event. The code is similar to this:

// The itemscontrol type is itemscontrol.

// Scrollviewer is of the scrollviewer type.

(Icollectionview) itemscontrol. Items). collectionchanged + = (sender, e) =>

{

Scrollviewer. scrolltoend ();

};

 

The second thing to note is getting scrollviewer from the itemscontrol control template. In fact, WPF programmers should be familiar with it, A helper function uses the visualtreehelper type of WPF to search for nodes of the scrollviewer type in the visual tree of the itemscontrol control template and return the result.

 

Download the source code of the current version
Download Page
Note: the link is the Microsoft SkyDrive page. When downloading, use a browser to download it directly. Some download tools may not be available for downloading.
Source code environment: Microsoft Visual Studio express 2012 for Windows Desktop

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.