WP8 _ check whether the list slides

Source: Internet
Author: User

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:

  1. First we attach a handler calledMainpage_loadedToPage. LoadedEvent
  2. When the page loads, we callFindsimplevisualchildTo getScrollviewerChild of the List (this is a pretty dumb function)
    1. We make sure to only do this once, because the page cocould get loaded more than once if it is navigated
  3. Then we callFindvisualstateTo get the named visual state from the first child ofScrollviewer
  4. 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

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.