我有一个Behavior<FrameworkElement>
,我在其中注册PreviewKeyDown
和PreviewMouseLeftButtonDown
。我已将此Behavior
附加到MenuItem
上。现在,当我点击MenuItem
时,有时仍会调用ClickEvent
,有时则不会。这就像一个计时问题。您对如何解决此问题有任何建议并停止调用event
吗?
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.PreviewKeyDown += _OnPreviewKeyDown;
AssociatedObject.PreviewMouseLeftButtonDown += _OnPreviewMouseLeftButtonDown;
}
private void _OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs mouseButtonEventArgs)
{
if (!_HasAccess())
mouseButtonEventArgs.Handled = true;
}
private void _OnPreviewKeyDown(object sender, KeyEventArgs keyEventArgs)
{
if (!_HasAccess())
keyEventArgs.Handled = true;
}
答案 0 :(得分:0)
我还必须订阅AssociatedObject.PreviewMouseLeftButtonUp
并设置handled = true
。现在我得到了理想的行为!