(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .
        Login with Facebook

Tip: How to handle the MouseLeftButtonDown and MouseLeftButtonUp events of the Button control?

(13 votes)
Martin Mihaylov
>
Martin Mihaylov
Joined Oct 29, 2007
Articles:   50
Comments:   70
More Articles
7 comments   /   posted on Nov 28, 2008
Tags:   button , martin-mihaylov
Categories:   Controls

Have you ever noticed that the MouseLeftButtonDown and MouseLeftButtonUp events are not fired when a Silverlight button is clicked? The reason for this is that the button handles these two events itself by overriding the OnMouseLeftButtonDown  and the OnMouseLeftButtonUp  handlers. In the OnMouseLeftButtonDown  override, the Click event is raised and the MouseLeftButtonDown event is marked as handled so it couldn't bubble in the visual tree. The OnMouseLeftButtonUp  override also marks the MouseLeftButtonUp  as handled.

This thing can be changed using the ClickMode property of the Button control. It has the following values - Hover, Press, Release. The default one is Pressed and we have already explained it. When we have ClickMode set to Release, the Click event will be raised in the OnMouseLeftButtonUp override and the MouseLeftButtonDown and MouseLeftButtonUp events will be handled inside the button again. If we set the ClickMode to Hover, the Click event will be raised with the MouseEnter event and we will also be able to use the mouse button events.

That's it!


Subscribe

Comments

  • -_-

    RE: Tip: How to handle the MouseLeftButtonDown and MouseLeftButtonUp events of the Button control?


    posted by Tim on Dec 30, 2008 17:16

    This worked great, better than making my own button class that derives from Button

  • -_-

    RE: Tip: How to handle the MouseLeftButtonDown and MouseLeftButtonUp events of the Button control?


    posted by Praveen on Feb 18, 2010 09:17
    Thanks for the tip
  • -_-

    RE: Tip: How to handle the MouseLeftButtonDown and MouseLeftButtonUp events of the Button control?


    posted by wuxy on Mar 31, 2010 12:12
    thanks
  • -_-

    RE: Tip: How to handle the MouseLeftButtonDown and MouseLeftButtonUp events of the Button control?


    posted by agui on Aug 18, 2010 04:40
    Thanks. That confuse me for a long time.
  • -_-

    RE: Tip: How to handle the MouseLeftButtonDown and MouseLeftButtonUp events of the Button control?


    posted by nono971 on Sep 23, 2010 18:24
    How to do it with Code Behind ?
  • Enrai

    RE: Tip: How to handle the MouseLeftButtonDown and MouseLeftButtonUp events of the Button control?


    posted by Enrai on Sep 23, 2010 18:29

    If you mean the setting of the ClickMode property, it's pretty easy. Just look at the following lines of code:

    Button b = new Button();
    b.ClickMode = ClickMode.Release;

    Greets!

  • -_-

    RE: Tip: How to handle the MouseLeftButtonDown and MouseLeftButtonUp events of the Button control?


    posted by Daniel Greitens on Apr 14, 2011 06:34

    You are not serious! What happens if a user touches a cancel button on a large form? Just firing what is connected to the cancel button??

    The magic has to do with focus-mechanism, coming from control. It will redirect all events to the focused element (in a stytic class deep in the framework). And on mousebuttondown, this focus is set via base.Focus();

    So: Just override MouseLeftButtonDown and set the focus and everything works fine.

    Example of a control that can be placed within a button (or a button template):

        public class InAction : ContentControl
        {
            public InAction()
            {
                this.DefaultStyleKey = typeof(InAction);
                this.MouseLeftButtonDown += new MouseButtonEventHandler(InAction_MouseLeftButtonDown);
            }
            void InAction_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                base.Focus();   
            }
        }

Add Comment

Login to comment:
  *      *       
Login with Facebook