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