locked
Which function can get the focus element? RRS feed

  • Question

  •  If a window has some control, I want to get the focus control, which function can get it? thanks.
    Tuesday, August 19, 2008 9:13 AM

Answers

  • Hi,

    The IsFocused property of FrameworkElement will tell you whether a particular element has focus or not. However, there is no mechanism for determining which of your elements currently has focus. To achieve this you have to walk the visual tree to locate the element where IsFocused==true.

    Fortunately you can find a useful visual tree helper class here:

    http://blogs.msdn.com/karstenj/archive/2006/04/19/579233.aspx

    Take a look at Helpers.FindFocusedElement, simply provide it with your root Visual element.

    Regards,
    Colin E.
    Tuesday, August 19, 2008 2:15 PM

All replies

  • Keyboard.FocusedElement should do the trick for you.

    You could also consider iterating through your controls and checking the isFocused property on each of them.

    Good luck,
    Emmanuel
    Tuesday, August 19, 2008 2:04 PM
  • Hi,

    The IsFocused property of FrameworkElement will tell you whether a particular element has focus or not. However, there is no mechanism for determining which of your elements currently has focus. To achieve this you have to walk the visual tree to locate the element where IsFocused==true.

    Fortunately you can find a useful visual tree helper class here:

    http://blogs.msdn.com/karstenj/archive/2006/04/19/579233.aspx

    Take a look at Helpers.FindFocusedElement, simply provide it with your root Visual element.

    Regards,
    Colin E.
    Tuesday, August 19, 2008 2:15 PM