Answered ContainsFocus

  • Thursday, July 30, 2009 6:17 AM
     
     

    Does WPF have a ContainsFocus counterpart?

    or is there a better way to determine if an element or its children contain the focus than travelling along the logical tree?

Answers

  • Thursday, July 30, 2009 6:39 AM
     
     Answered
    Hi, you can always determine which element has logical focus in your application through the FocusManager.GetFocusedElement method -- pass it the window in question and it will return which element has logical focus in that window.  Remember that logical focus != keyboard focus at all times -- toolbars and menus track their own focus so if you are currently interacting with a menu then the menu has physical focus.  But in general, the following code will tell you which element WPF thinks has focus in the window:

    IInputElement focusedElement = FocusManager.GetFocusedElement(thisWindow);

    To determine whether this element has keyboard focus, we can check the IsKeyboardFocused property - if it's set to true, then that element currently has the keyboard focus (as well as being the logical focus for that focus scope).


    Oscar Avarez Guerras - Arquitecto Software en I3B (I+D+I) Blog:http://geeks.ms/blogs/oalvarez Por favor marca como respuesta si te ha ayudado esta respuesta

All Replies

  • Thursday, July 30, 2009 6:39 AM
     
     Answered
    Hi, you can always determine which element has logical focus in your application through the FocusManager.GetFocusedElement method -- pass it the window in question and it will return which element has logical focus in that window.  Remember that logical focus != keyboard focus at all times -- toolbars and menus track their own focus so if you are currently interacting with a menu then the menu has physical focus.  But in general, the following code will tell you which element WPF thinks has focus in the window:

    IInputElement focusedElement = FocusManager.GetFocusedElement(thisWindow);

    To determine whether this element has keyboard focus, we can check the IsKeyboardFocused property - if it's set to true, then that element currently has the keyboard focus (as well as being the logical focus for that focus scope).


    Oscar Avarez Guerras - Arquitecto Software en I3B (I+D+I) Blog:http://geeks.ms/blogs/oalvarez Por favor marca como respuesta si te ha ayudado esta respuesta
  • Thursday, July 30, 2009 8:54 AM
     
     
    You're probably looking for the IsKeyboardFocusWithin property. That returns true if the element has keyboard focus or one of its children (or childrens' children etc) do.
    Controls for WPF, Windows Forms and Silverlight at http://www.divelements.co.uk
  • Monday, November 07, 2011 4:03 PM
     
     
    A simple thank you !!!