更新:Discusion显示只有在wpf应用程序中托管自定义控件文本框时才会出现问题,该应用程序再次通过winforms应用程序中的elementhost托管。
我有一个从TextBox开始的WPF-CustomControl。我重写了OnLostKeyBoardFocus方法。
作为这种方法的一部分,我举起了一个活动。一个事件处理程序显示一个MessageBox(这不在我的控制之下)。当用户关闭MessageBox时,KeyBoardFocus将直接返回到TextBox。尽管如此,OnLostKeyboardFocus(...)仍然没有返回。
自动(重新)聚焦我的TextBox控件,给我带来了一系列问题。除了使用Dispatcher.BeginInvoke()调度事件之外,我是否可以通过某种方式规避此行为。
class MyTextBoxCustomControl : TextBox {
public event EventHandler<EditCompletedEventArgs> EditCompleted;
private void OnEditCompleted(EditCompletedEventArgs e)
{
var handler = EditCompleted;
if (handler != null) handler(this, e);
}
protected override OnLostKeyboardFocus(KeyboardFocusChangedEventArgs e){
base.OnLostKeyboardFocus(e);
OnEditCompleted(new EditCompletedEventArgs())
//Before this point is reached OnGotKeyboardFocus(...) is called
}
protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
base.OnGotKeyboardFocus(e);
//Is called twice, directly after MessageBox is closed and
//after OnLostKeyboardFocus(...) returns
}
}
class MyEventHandler {
private void Test(){
var myTBCC = new MyTextBoxCustomControl();
//Closing the message box will return focus to myTBCC, which directly
//causes OnGotKeyboardFocus to be called
myTBCC.EditCompleted += (a, b) =>
MessageBox.Show("PressOk");
}
}
答案 0 :(得分:1)
您可以在MessageBox.Show。
之后立即尝试在发件人上发送.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
修改强>
...
MessageBox.Show("PressOk");
((MyTextBoxCustomControl)a).MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
...