我正在尝试制作自己的 wpf TextBox
,当它获得焦点时捕获鼠标。我的代码:
public FocusedTextBox()
{
AddHandler(Mouse.PreviewMouseDownOutsideCapturedElementEvent, new MouseButtonEventHandler(OnPreviewMouseDownOutsideCapturedElement));
}
protected override void OnGotFocus(RoutedEventArgs e)
{
base.OnGotFocus(e);
this.CaptureMouse();
}
protected override void OnLostMouseCapture(MouseEventArgs e)
{
base.OnLostMouseCapture(e);
}
但是立即OnGotFocus
结束,该OnLostMouseCapture
方法被调用,我不知道为什么。但在OnLostMouseCapture
两者内部FocusManager.GetFocuseElement(window)
并Keyboard.FocuseElement
返回FocusedTextBox
. 如何保持鼠标捕获FocusedTextBox
?