简体   繁体   English

TextBox LostFocus事件经常发生

[英]TextBox LostFocus event occurs too often

I have an application in Silverlight and WPF. 我在Silverlight和WPF中有一个应用程序。 The error just happens in Silverlight, but same code is used in WPF. 该错误只发生在Silverlight中,但WPF中使用了相同的代码。

In my application, there is a RibbonBar , with several RibbonGroups . 在我的应用程序,有一个RibbonBar ,有几个RibbonGroups In each RibbonGroup there are at least one RibbonButton . 在每个RibbonGroup中至少有一个RibbonButton One of those RibbonGroups also contains four TextBox . 其中一个RibbonGroups还包含四个TextBox

Every TextBox has its own OnLostFocus -Handler. 每个TextBox都有自己的OnLostFocus -Handler。 When I leave a TextBox the related Handler is raised. 当我离开TextBox ,会引发相关的Handler。

Now, (1) I click into one of those TextBoxes and (2) then click a RibbonButton, OnLostFocus raises and after that the RibbonButton dropdown menu appears. 现在,(1)我点击其中一个TextBoxes和(2)然后点击RibbonButton,OnLostFocus加注,然后出现RibbonButton下拉菜单。 Everything OK. 一切还好。

After that, (3) I click on another RibbonButton. 之后,(3)我点击另一个RibbonButton。 Again OnLostFocus raise, although - imho - it shouldn't. 再次OnLostFocus加注,虽然 - imho - 它不应该。

So it goes on and on. 所以它一直在继续。 After every action the TextBox keeps(or gets back, don't know) the focus and this causes the OnLostFocus-Handler to raise. 在每次操作之后,TextBox保持(或者返回,不知道)焦点,这会导致OnLostFocus-Handler引发。

In WPF same code does not raise OnLostFocus again. 在WPF中,相同的代码不会再次引发OnLostFocus。 Just one time, when it really lost focus. 只有一次,它真的失去了焦点。

Does anybody know, why this behaviour is that strange in Silverlight. 有谁知道,为什么这种行为在Silverlight中很奇怪。 What is really different to WPF, maybe I just have to set a property which default value ist different to WPF. 与WPF有什么不同,也许我只需设置一个默认值与WPF不同的属性。

Thanks in advance. 提前致谢。

Try using OnPreviewLostKeyboardFocus. 尝试使用OnPreviewLostKeyboardFocus。 OnLostFocus is for logical focus which you may lose for a variety reasons. OnLostFocus用于逻辑焦点,您可能会因各种原因而丢失。

I don't know the reason for sure but I suspect that the problem you're seeing might be because the ribbon bar is in a FocusScope. 我不知道确定的原因,但我怀疑你看到的问题可能是因为功能区栏位于FocusScope中。 When you put focus onto something inside of a focus scope then what you end up with can seem like focus bouncing around some. 当你将焦点放在焦点范围内的某些东西上时,你最终得到的东西看起来就像聚焦在一些物体周围。 I'm not certain of this answer, focus is very complicated in WPF. 我不确定这个答案,WPF中的焦点非常复杂。 Submitting some code samples might help. 提交一些代码示例可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM