Home > database >  WPF button OnClick OnPreviewMouseDown cancel it
WPF button OnClick OnPreviewMouseDown cancel it

Time:09-19

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 onm ouseClick(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 onm ouseClick event.

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

CodePudding user response:

In OnPreviewMouseDown, tell that the click is Handled..

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