How to add a double-click event for Silverlight in Silverlight development

Source: Internet
Author: User
Tags definition silverlight

The events of the mouse in the daily development are the most common events we use, but in Silverlight, only the following six limited mouse events are supported: MouseEnter, MouseLeave, MouseLeftButtonDown, MouseLeftButtonUp, MouseMove, MouseWheel. This has created a great problem for our development, but it's good that Silverlight supports a powerful additional property mechanism, and here's how to add a mouse double-click event to Silverlight with additional properties.

The attached property is one of the most innovative and powerful features of Silverlight and WPF that allows you to extend the functionality without owning the source code of the control. About the strength of the attached property I'm not here to describe, there are interested friends can go to see WPF control development Mystery Book, there is a special contention on the attached properties, the network and the garden has many articles on the attached properties.

Here directly into the theme, first of all, we need to provide two kinds of double click event Processing mechanism, one is commonly used event mechanism, use Mousebuttoneventhandler, another is now more popular command mechanism, using the ICommand interface. The two additional properties are defined as follows:

Definition of attached property

#region MouseDoubleClick
public static Mousebuttoneventhandler Getmousedoubleclick (DependencyObject obj)
{
Return (Mousebuttoneventhandler) obj. GetValue (Mousedoubleclickproperty);
}
public static void Setmousedoubleclick (DependencyObject obj, Mousebuttoneventhandler value)
{
Obj. SetValue (mousedoubleclickproperty, value);
}
public static readonly DependencyProperty Mousedoubleclickproperty = dependencyproperty.registerattached (
"MouseDoubleClick",
typeof (Mousebuttoneventhandler),
typeof (Mouseeventhelper),
New PropertyMetadata (New Propertychangedcallback (onmousedoubleclickchanged)));
#endregion
#region Mousedoubleclickcommand
public static ICommand Getmousedoubleclickcommand (DependencyObject obj)
{
Return (ICommand) obj. GetValue (Mousedoubleclickcommandproperty);
}
public static void Setmousedoubleclickcommand (DependencyObject obj, ICommand value)
{
Obj. SetValue (mousedoubleclickcommandproperty, value);
}
public static readonly DependencyProperty Mousedoubleclickcommandproperty =
Dependencyproperty.registerattached ("Mousedoubleclickcommand", typeof (ICommand), typeof (Mouseeventhelper), new PropertyMetadata (onmousedoubleclickchanged));
#endregion

The main difference between the defined and dependent properties of an attached property is that the attached property is registered by dependencyproperty.registerattached, and the dependent property is registered through the Dependencyproperty.register method. The Get and set methods are also provided here, which are provided for use when attaching property registrations in XAML code. When you are defining additional properties, you can create code by typing Propa and then pressing the TAB key. You can note that in the definition of two attributes we have all passed in a propertychangedcallback, which is called an extension point, in which we can deal with our own logic, where we just The UIElement MouseLeftButtonDown event is registered so that our attached properties can be applied to all controls that inherit from UIElement (basically the entire 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.