WPF 模态窗口行为怪异
我遇到了我不明白的情况,希望有人能启发我(如果可能的话)。
创建了一个 WPF 应用程序。为了简单起见,请将此应用程序视为一个窗口。我在此窗口上放置了一些控件,其中有一个 TextBox 控件(我们称之为 TB)。
要求该 TB 必须始终具有焦点(如果有人键入某些内容或通过条形码扫描仪读取某些内容,它应该出现在那里)。
我想借助计时器来实现这一点:每一秒焦点都会转移到 TB。
到目前为止,一切正常(如预期)。
奇怪的事情如下:假设一个新用户想要使用这个窗口,所以她必须登录。我想用一个新的 Window 对象(我们称之为 W2)来实现它,这样调用它:
W2.ShowDialog();
这个 W2 窗口应该是模态的(请记住计时器仍然执行)。
疯狂的是,在Windows Vista下,它可以按预期完美工作(这意味着 W2 是模态的,用户可以在 W2 上执行她应该做的事情),但是 在 Windows XP 上,一旦用户想要在 W2 的文本框中键入内容,焦点就会重新设置回初始窗口上的 TB(据我所知:这意味着 W2 不是)强> 模态!!!?我说得对吗?)。
我怎样才能克服这种情况?
这是正确的方法吗?
提前致谢
I 'm running into a situation i don't understand and want somebody to enlighten me if possible.
Created a WPF application. For sake of simplicity consider this application to be one window. I put some controls on this window, amongst others there is a TextBox control (let's call it TB).
There is a requirement that this TB must always have the focus (in case someone types something or something is read through the barcode-scanner it should appear there).
I thought implementing this with the help of a timer: every second the focus is transferred to the TB.
Until now everything works fine (as expected).
The weird thing is the following: let's say a new user wants to use this window, so she has to sign in. I thought implementing this with a new Window object (let's call it W2), calling it this way:
W2.ShowDialog();
This W2 window should be modal (bear in mind that the timer still executes).
The crazy thing is that under Windows Vista this works perfectly as expected (this means W2 is modal and the user can to what she should do on W2), BUT on Windows XP as soon as the user wants to type something in a textbox of W2 the focus is set back to TB on the initial Window (as far as i can interpret: this means W2 is not modal!!!? am i right?).
How can i overcome this situation?
Is this the right approach?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保设置所有者属性 W2 到您的主窗口。
从参考文档中:
Make sure to set the Owner property of W2 to your main Window.
From the referenced docs:
我不确定每隔几秒将焦点重新设置一次是否是一个好主意。在失去焦点状态和获得焦点状态(并且丢失)之间的间隔中总是可能出现一些输入。替代解决方案是捕获 TB 所在窗体上的 PreviewKeydown 事件,并以某种方式将条形扫描仪的输出也设置到 TB。
I am not sure this setting-the-focus-back-every-few-seconds is such a good idea. It is always possible that some input will come in the interval between a lost and got focus state (and be lost). Alternative solutions are catching the PreviewKeydown event on the form that the TB resides on and somehow set the output of a barscanner to the TB too.