none
Why don't the controls lose their focus when menu is clicked? RRS feed

  • Question

  • I have data-bound TextBoxes. I used the default update method, because PropertyChanged events occurs too much (on every key stroke).

     

    When a TextBox loses its focus to another TextBox, databinding occurs. But against my expectations, it didn't occur when the user clicks a menu. This is a big problem because when the user types some text into a TextBox, then clicks File->Save menu, the model is still not updated because databinding did not occur.

    What is the general solution? Didn't the TextBox lose its focus to the menu? Why doesn't the databinding occur?

     

    Saturday, May 14, 2011 11:53 AM

Answers

  • Hi,

    you could use a BindingGroup on a container around all the controls that need to be updated, and call BindingGroup.UpdateSources before you do anything else in the menu handler.


    http://wpfglue.wordpress.com
    Saturday, May 14, 2011 6:25 PM
  • Hi,

    In addition to PaulSinnema's explanation, the menu is actually restoring the keyboard focus to the previous focus scope on purpose. This is a feature of the WPF. It allows the commands of the menu item to fire at the previous focused element.

    For example, if you select some text in a TextBox and click the Cut command in the Menu, the Cut command will be handled by the TextBox not the Menu itself.

    You can set menu's FocusManager.IsFocusScope property to turn off this feature. But it will cause glitches in the Menu's visual styles.

    I think hbarck's suggestion is a good solution.

    If you still have any questions or concerns about this issue, please feel free to let me know.

    Best regards,

     


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 17, 2011 7:49 AM
    Moderator
  • Hi,

    You should read about the focus (and scope) mechanisms of WPF (http://msdn.microsoft.com/en-us/library/aa969768.aspx). There are 2 kinds of focus. The logical Focus: this is the control that has the focus within a scope. The keyboard focus: this is the control that receives the keyboard input. A control in a scope can have both the logical and keyboard focus. When the keyboard focus moves to another scope the control still has the logical focus but loses the keyboard focus. WPF shows this by still showing the focus color on the control that has the logical focus. A menu is not focusable (can not take the keyboard focus). That's why a control doesn't loose focus when a menu is clicked.

    Regards
    Paul

     


    Take a look at my blog: http://paul.sinnema.ch

    Saturday, May 14, 2011 4:56 PM

All replies

  • Hi,

    You should read about the focus (and scope) mechanisms of WPF (http://msdn.microsoft.com/en-us/library/aa969768.aspx). There are 2 kinds of focus. The logical Focus: this is the control that has the focus within a scope. The keyboard focus: this is the control that receives the keyboard input. A control in a scope can have both the logical and keyboard focus. When the keyboard focus moves to another scope the control still has the logical focus but loses the keyboard focus. WPF shows this by still showing the focus color on the control that has the logical focus. A menu is not focusable (can not take the keyboard focus). That's why a control doesn't loose focus when a menu is clicked.

    Regards
    Paul

     


    Take a look at my blog: http://paul.sinnema.ch

    Saturday, May 14, 2011 4:56 PM
  • Thanks, it describes why it happens, but there seems to be no solution to solve the problem?

     

    I think this is a common problem. Binded textbox, user typed something, users moves to the save menu...

     

    Probably people have already encountered this issue. So how did you solve it?

    Saturday, May 14, 2011 5:43 PM
  • Hi,

    you could use a BindingGroup on a container around all the controls that need to be updated, and call BindingGroup.UpdateSources before you do anything else in the menu handler.


    http://wpfglue.wordpress.com
    Saturday, May 14, 2011 6:25 PM
  • Hi,

    In addition to PaulSinnema's explanation, the menu is actually restoring the keyboard focus to the previous focus scope on purpose. This is a feature of the WPF. It allows the commands of the menu item to fire at the previous focused element.

    For example, if you select some text in a TextBox and click the Cut command in the Menu, the Cut command will be handled by the TextBox not the Menu itself.

    You can set menu's FocusManager.IsFocusScope property to turn off this feature. But it will cause glitches in the Menu's visual styles.

    I think hbarck's suggestion is a good solution.

    If you still have any questions or concerns about this issue, please feel free to let me know.

    Best regards,

     


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 17, 2011 7:49 AM
    Moderator