One of the UI features of lists on Windows Phone 7 is that the "scroll bars" don't really act like traditional scroll bars; they are non-interactive and they only appear when the list is actually scrolling. to achieve this, the Silverlight team added a new visual state group that is used in the default control template for showing/hiding the scroll bars.
You can get programmatic access to the scroll state with the following approach. First, paste this XAML intoContentgridOf a new WP7 application:
<ListBox fontsize = "50" X: Name = "theList">
<Listboxitem content = "item 00"/>
<Listboxitem content = "item 01"/>
<Listboxitem content = "item 02"/>
<Listboxitem content = "item 03"/>
<Listboxitem content = "item 04"/>
<Listboxitem content = "item 05"/>
<Listboxitem content = "item 06"/>
<Listboxitem content = "item 07"/>
<Listboxitem content = "item 08"/>
<Listboxitem content = "item 09"/>
<Listboxitem content = "Item 10"/>
<Listboxitem content = "item 11"/>
<Listboxitem content = "item 12"/>
<Listboxitem content = "Item 13"/>
<Listboxitem content = "item 14"/>
<Listboxitem content = "Item 15"/>
<Listboxitem content = "item 16"/>
<Listboxitem content = "item 17"/>
<Listboxitem content = "item 18"/>
<Listboxitem content = "item 19"/>
<Listboxitem content = "item 20"/>
<Listboxitem content = "item 21"/>
<Listboxitem content = "item 22"/>
<Listboxitem content = "item 23"/>
<Listboxitem content = "item 24"/>
<Listboxitem content = "item 25"/>
<Listboxitem content = "item 26"/>
<Listboxitem content = "item 27"/>
<Listboxitem content = "item 28"/>
<Listboxitem content = "item 29"/>
</ListBox>
Now add the following to the code-behind file:
Public mainpage ()
{
Initializecomponent ();
Loaded + = new routedeventhandler (Mainpage_loaded);
}
Bool alreadyhookedscrollevents = false;
VoidMainpage_loaded(Object sender, routedeventargs E)
{
If (alreadyhookedscrollevents)
Return;
Alreadyhookedscrollevents = true;
Scrollviewer viewer =Findsimplevisualchild<Scrollviewer> (theList );
If (viewer! = NULL)
{
// Visual states are always on the first child of the control Template
Frameworkelement element = visualtreehelper. getchild (viewer, 0) as frameworkelement;
If (element! = NULL)
{
Visualstategroup group =Findvisualstate(Element, "scrollstates ");
If (group! = NULL)
{
Group.Currentstatechanging+ = (S, argS) => pagetitle. Text = args. newstate. Name;
}
}
}
}
VisualstategroupFindvisualstate(Frameworkelement element, string name)
{
If (element = NULL)
Return NULL;
Ilist groups = visualstatemanager. getvisualstategroups (element );
Foreach (visualstategroup group in groups)
If (group. Name = Name)
Return group;
Return NULL;
}
TFindsimplevisualchild<T> (dependencyobject element) where T: Class
{
While (element! = NULL)
{
If (element is T)
Return element as t;
Element = visualtreehelper. getchild (element, 0 );
}
Return NULL;
}
What this does is attach toCurrentstatechangingEvent ofVisualstategroup, Which will be raised every time the scroll state changes from "scrolling" to "notscrolling" or vice-versa. there's a bunch of infrastructure code to walk the visual tree and pull out a state group, but the core code is very simple:
- First we attach a handler calledMainpage_loadedToPage. LoadedEvent
- When the page loads, we callFindsimplevisualchildTo getScrollviewerChild of the List (this is a pretty dumb function)
- We make sure to only do this once, because the page cocould get loaded more than once if it is navigated
- Then we callFindvisualstateTo get the named visual state from the first child ofScrollviewer
- Then we add a handler toCurrentstatechangingEvent
Peter Torr-MSFT
Link: http://blogs.msdn.com/ B /ptorr/archive/2010/07/23/how-to-detect-when-a-list-is-scrolling-or-not.aspx
WP8 _ check whether the list slides