locked
Disable mouse click, how to? RRS feed

  • Question

  • Hi guys, I would disable mouse click on a specific UI element, How could I do this?

    I'll try to explain what's wrong in the default beahviour.

    I've created a xaml (named CustomControl) which contain a border. The border contains a button.

    Then the control is inserted into another xaml, called MainContainer.

    In the MainContainer code behind I've registered an handler to listen mouse click event from the CustomControl. In this case I use RoutedEvent flow. [I use RoutedEvent mechanism instead of a custom event]

     
    this.AddHandler(UIElement.MouseLeftButtonDown, new MouseEventHandler(myfunction), true);
     

    The code works well even if myfunction is called both when I click the button and when I click the border. So, is it possible to disable to listen for click events for the border control?

     Thanks  a lot. Regards.

    Thursday, June 10, 2010 4:19 PM

Answers

  • In your custom control define an event of MouseEventHandler and raise it to be caught in any parent container (mainpage).

     

    public event MouseEventHandler CustomEventHandlerInMainForm;

    In your event handler code write following code

    if (CustomEventHandlerInMainForm != null)
          CustomEventHandlerInMainForm(sender, e);

     

    In main page where you use control register this event handler...

     

    CustomControl customControl = new CustomControl();

    customControl.CustomEventHandlerInMainForm += new System.Windows.Input.MouseEventHandler(customControl_CustomEventHandlerInMainForm);

    Friday, June 11, 2010 7:49 AM

All replies

  •  register your eventhandler for button instead of the entire control.

    root = (FrameworkElement)VisualTreeHelper.GetChild(this, 0);

      button = root.FindName("YourButtonName") as FrameworkElement;

     

    Or best would be to add eventhandler within your custom control where you can add the handler like following

    btnYourButtonControl.

    AddHandler(UIElement.MouseLeftButtonDown, new MouseEventHandler(myfunction), true);
    Thursday, June 10, 2010 4:59 PM

  • Or best would be to add eventhandler within your custom control where you can add the handler like following

    btnYourButtonControl.

    AddHandler(UIElement.MouseLeftButtonDown, new MouseEventHandler(myfunction), true);

     

    Could yoy explian the second solution? I would the event bubbles to the MainContainer? Thanks a lot.

    Friday, June 11, 2010 4:07 AM
  • In your custom control define an event of MouseEventHandler and raise it to be caught in any parent container (mainpage).

     

    public event MouseEventHandler CustomEventHandlerInMainForm;

    In your event handler code write following code

    if (CustomEventHandlerInMainForm != null)
          CustomEventHandlerInMainForm(sender, e);

     

    In main page where you use control register this event handler...

     

    CustomControl customControl = new CustomControl();

    customControl.CustomEventHandlerInMainForm += new System.Windows.Input.MouseEventHandler(customControl_CustomEventHandlerInMainForm);

    Friday, June 11, 2010 7:49 AM