1

我想检测用户何时在自定义网格之外单击,但是当用户单击网格时,我总是会收到 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);
        }
    }
4

1 回答 1

4

您可以使用该Mouse.Capture(...)方法来获取未引用您的网格的鼠标事件。如果您正在绘制线条或其他任何东西,这将非常有用。看看这个:Mouse.Capture on msdn

于 2012-08-27T09:27:45.383 回答