[SOLVED] WPF button OnClick OnPreviewMouseDown cancel it – Stack Overflow

Issue

This Content is from Stack Overflow. Question asked by Zaken

I have a situation where if a user click on a mouse button. I want to cancel the mouse click base on certain situation. Like if he clicks on a button when say a certain state is not met, the clicked button event is not fired. So what i did is i add an OnPreviewMouseDown event and check if the situation is met, if not I need to cancel the click event, how do i cancel the users mouse click event in this case?

public OnMouseClick(object sendor, RoutedEventArgs e)
{
     // do some work.
}

public OnPreviewMouseClick(object sender, MouseButtonEventArgs e)
{
     if (condition_not_met)
     {
         // how do i enter code here to cancel the mouse click event so it WILL NOT
         // fire the OnMouseClick event.

         // there is no such thing as e.cancel = true or 
         var btn = sender as Button;
         btn.cancel;
     }
}



Solution

In OnPreviewMouseDown, tell that the click is Handled..

if (condition_not_met)
{
    e.Handled = true;
    // ..
}


This Question was asked in StackOverflow by Zaken and Answered by Muhammad Sulaiman It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?