XAML WPF按钮单击单击预览按下鼠标取消它

nimxete2  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(181)

我遇到了一个情况,如果用户单击鼠标按钮。我希望根据特定情况取消鼠标单击。例如,如果用户在不满足特定状态时单击按钮,则不会触发单击按钮事件。因此,我所做的是添加一个OnPreviewMouseDown事件,并检查是否满足该情况,如果不满足,则需要取消单击事件。在这种情况下,如何取消用户的鼠标单击事件?

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;
     }
}
5w9g7ksd

5w9g7ksd1#

OnPreviewMouseDown中,告知单击已处理。

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

相关问题