Windows Phone 7: how to determine whether the ListBox control is rolled to the end

Source: Internet
Author: User
If a large amount of data is bound to The ListBox control, loading is usually slow, there is an interactive solution to optimize this situation, that is, loading part of the data on the ListBox first, some data will be loaded when you scroll down The ListBox to the end. However, there are no related events or attributes in The ListBox control to determine when the ListBox is rolled to the end. The following describes a solution. The ListBox control encapsulates the scrollviewer control and scrollbar control. This is easy to handle. You can obtain the scrollviewer control encapsulated in the ListBox control, and then determine whether the ListBox control is rolled to the end through the attributes of the scrollviewer control. Next let's take a look. Code :
 <  Grid  X: Name  = "Contentpanel"  Grid. Row  = "1"  Margin  = "12, 0, 12, 0"  > 
< ListBox Name = "Listbox1" Mousemove = "Listbox#mousemove" >
</ ListBox >
</ Grid >
Using  System;
Using System. Collections. Generic;
Using System. windows;
Using System. Windows. controls;
Using System. Windows. input;
Using System. Windows. Media;
Using Microsoft. Phone. controls;

Namespace Listboxupdate
{
Public Partial Class Mainpage: phoneapplicationpage
{
Public Mainpage ()
{
Initializecomponent ();

For ( Int I = 0 ; I < 30 ; I ++ )
{
Listbox1.items. Add ( " Project " + I );
}

}

Private Void Listbox1_mousemove ( Object Sender, mouseeventargs E)
{
// Obtain the subtype scrollviewer of ListBox
Scrollviewer = Findchildoftype < Scrollviewer > (Listbox1 ); // Scrollviewer scrollbar
If (Scrollviewer = Null )
{
Throw New Invalidoperationexception ( " Erro " );
}
Else
{
// Determines whether the current rolling height is greater than or equal to the actual rolling height of scrollviewer.
If (Scrollviewer. verticaloffset > = Scrollviewer. scrollableheight)
{
// Handling ListBox scrolling to the end
For ( Int I = 0 ; I < 10 ; I ++ )
{
Int K = Listbox1.items. count;
Listbox1.items. Add ( " Project " + K );
K ++ ;
}
}
}
}

// Obtain child types
Static T findchildoftype < T > (Dependencyobject root) Where T: Class
{
VaR queue = New Queue < Dependencyobject > ();
Queue. enqueue (Root );

While (Queue. Count > 0 )
{
Dependencyobject current = Queue. dequeue ();
For ( Int I = Visualtreehelper. getchildrencount (current) - 1 ; 0 <= I; I -- )
{
VaR child = Visualtreehelper. getchild (current, I );
VaR typedchild = Child As T;
If (Typedchild ! = Null )
{
Return Typedchild;
}
Queue. enqueue (child );
}
}
Return Null ;
}
}
}
Running Effect Do you have any better solutions ???     Below there is a person recommended to use an extended ListBox control lazylistbox, find the blog link: http://blogs.msdn.com/ B /ptorr/archive/2010/10/12/procrastination-ftw-lazylistbox-should-improve-your-scrolling-performance-and-responsiveness.aspx
Related Article

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.