WPF网格检测外部点击

时间:2012-08-27 09:23:27

标签: wpf grid lostfocus

我想检测用户何时在自定义网格外部点击,但是当用户点击网格时,我总是收到LostFocus事件。可聚焦是真的,但似乎网格永远不会成为焦点。有人可以帮帮我吗?

public class GridEditor : Grid
{
    public GridEditor()
    {
        Loaded += GridEditor_Loaded;
    }

    private void GridEditor_Loaded(object sender, RoutedEventArgs e)
    {
        Focusable = true;

        this.LostFocus += new RoutedEventHandler(GridEditor_LostFocus);
    }

    void GridEditor_LostFocus(object sender, RoutedEventArgs e)
    {
        if (!this.IsKeyboardFocusWithin && !this.IsMouseOver)
        {
            Commands.EditConfirmed.Execute(DataContext, this);
        }
    }

1 个答案:

答案 0 :(得分:4)

您可以使用Mouse.Capture(...)方法获取未引用网格的鼠标事件。如果您正在绘制线条或其他任何内容,这可能非常有用。 请看一下:Mouse.Capture on msdn