Wednesday, April 20, 2011

UserControl closes when focus is lost

I have a UserControl that I want it to close when focus is lost from it completely. Knowing that it has few children elements: TextBox and few buttons.

Obstacles:
- if I click on button on this UserControl , it loses the focus immediately.
So any attempt to handle lost focus to close the control will cause the control to close every time you click any button on it.

SOLUTION: do not use IsFocusScope unless the control is toolbar or menu (that should not maintain focus. Otherwise focus will keep returning to the first control of a non-IsFocusScope-host when you press any button on this IsFocusScope-host.