Add small classes supported by the mouse wheel for the Silverlight Control

Source: Internet
Author: User
Tags reflector
The http://www.cnblogs.com/ready4tech/archive/2009/02/13/MouseWheelSupportAddOn.html adds small classes that are supported by the mouse wheel for the Silverlight Control

 

 

 

In fact, there are a lot of articles on the Internet that support the scroll wheel. The principles are the same, through HtmlPage. window. attachEvent ("DOMMouseScroll ",...) to control the ScrollViewer, but many of them are limited to adding scroll wheel support to the ScrollViewer, while the controls such as TextBox and ListBox seem quite different.

Use Reflector to view the implementation of TextBox and ListBox, and find that there is a field inside them as ScrollViewer. As long as you read this field, you should be able to control their scrolling. I tried to use reflection to get their ScrollViewer, but the security mechanism of Silverlight is different from that of the full version of CLR. It does not allow me to get non-public members...

Suddenly... from Reflector, we can see that both TextBox and ListBox use the GetTemplateChild method of the parent class to obtain the ScrollViewer object, while GetTemplateChild is the protected method. Therefore, you only need to create a class that inherits the TextBox or ListBox, then use GetTemplateChild to expose their ScrollViewer!

Namespace Xin. Silverlight. MouseWheelSupport
{
Public class ListBox: System. Windows. Controls. ListBox, IScrollable
{
ScrollViewer sw;

Public ScrollViewer
{
Get
{
If (sw = null)
{
Sw = GetTemplateChild ("ScrollViewer") as ScrollViewer;
}
Return sw;
}
}
}
}

In the code, IScrollable is a small interface written by itself, and declares that the class to implement it must have a ScrollViewer property. In the sample code, this interface is only implemented for TextBox and ListBox. To add scroll wheel support for controls such as GridView, you only need to implement this interface and expose its ScrollViewer object.

After a little bit of code, you only need to executeMouseWheelSupportAddOn. Activate (this. LayoutRoot, true)You can add scroll wheel support to the control that supports scroll wheel for the entire Page.

ActivateThe method can accept ContentControl, Panel, and IScrollable objects. The second parameter specifies whether to add scroll wheel support to the Children of the control.

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.