简体   繁体   中英

TextBox LostFocus event occurs too often

I have an application in Silverlight and WPF. The error just happens in Silverlight, but same code is used in WPF.

In my application, there is a RibbonBar , with several RibbonGroups . In each RibbonGroup there are at least one RibbonButton . One of those RibbonGroups also contains four TextBox .

Every TextBox has its own OnLostFocus -Handler. When I leave a TextBox the related Handler is raised.

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. Everything OK.

After that, (3) I click on another RibbonButton. Again OnLostFocus raise, although - imho - it shouldn't.

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.

In WPF same code does not raise OnLostFocus again. Just one time, when it really lost focus.

Does anybody know, why this behaviour is that strange in Silverlight. What is really different to WPF, maybe I just have to set a property which default value ist different to WPF.

Thanks in advance.

Try using OnPreviewLostKeyboardFocus. OnLostFocus is for logical focus which you may lose for a variety reasons.

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. 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. Submitting some code samples might help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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