2

使用. .NET 4.5.1_ Windows 8.1 Pro在我的UserControl我有网格。我需要在这个网格中预览和处理鼠标事件。所以我覆盖了这个PreviewMouseLeftButtonDown事件:

    myGrid.PreviewMouseLeftButtonDown +=
        new MouseButtonEventHandler(myGrid_PreviewLeftButtonDown);
}

private void myGrid_PreviewLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    // ...
    e.Handled = true; // <-- IMPORTANT!
}

UserControl也有一些InputBindings正在静态构造函数中注册。例如:

CommandManager.RegisterClassInputBinding(typeof(MyUserControl),
    new KeyBinding(MyCommands.SelectAll, new KeyGesture(Key.A, ModifierKeys.Control)));

InputBinding依赖PreviewMouseLeftButtonDown?!

现在,当我在我的处理程序中设置时,我e.Handled的s 停止工作!为什么?!我无法理解鼠标处理程序如何链接到我的键盘快捷键!truePreviewMouseLeftButtonDownInputBinding

4

2 回答 2

2

我现在可以提出的唯一原因如下:您将输入绑定注册到MyUserControl,因此要激活此 UserControl 必须具有 focus。当您在网格级别处理鼠标事件时,您不允许此输入转到 UserControl。所以,没有输入 - 没有焦点,没有焦点 - 没有命令激活。

假设我们稍微修改一下代码:

private void Grid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    e.Handled = true;
    FocusManager.SetFocusedElement(this, myUC);
}

myUC我们的用户控件在哪里。但是 UserControl 是不可聚焦的,所以我们将在其中添加一点焦点转移:

protected override void OnGotFocus(RoutedEventArgs e)
{
    btn.Focus();
    base.OnGotFocus(e);
}

我们btn只是一些空按钮或您喜欢的任何可聚焦元素。然后它工作正常。

所以,我对这个问题的看法是:原因不是鼠标事件,而是焦点

于 2014-08-29T08:21:55.040 回答
1

PreviewMouseLeftButtonDown 在所有其他路由事件之前触发。当你在这里设置它 e.Handled = true 时,你有效地阻止了其他事件被触发。这也解释了其他 PreviewX 路由事件,目的是阻止它。不过,您可以在代码中添加处理程序,无论它们是否设置为已处理,都会获取这些事件。对于不活动检查等非常方便。

像这样:

AddHandler(MouseUpEvent, new MouseButtonEventHandler(YourHandler), true);


private void YourHandler(object sender, MouseButtonEventArgs e)
{
    // Do your magic here!
}

那里的最后一个参数将导致您捕获 MouseUpEvents,即使它们已被处理。

我不确定如何使用开箱即用的绑定来做到这一点,但它可能是可能的。

希望它至少能有所帮助。

干杯

斯蒂安

于 2014-08-28T14:44:11.197 回答