locked
Which function can get the focus element? RRS feed

  • Question

  •  If a window has some controls, 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.




    • Proposed as answer by Colin Eberhardt Tuesday, August 19, 2008 1:31 PM
    • Marked as answer by Marco Zhou Monday, August 25, 2008 10:31 AM
    Tuesday, August 19, 2008 1:31 PM

All replies

  •  

    txtCustomerCode.Focus()


    Mathi
    Tuesday, August 19, 2008 1:11 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.




    • Proposed as answer by Colin Eberhardt Tuesday, August 19, 2008 1:31 PM
    • Marked as answer by Marco Zhou Monday, August 25, 2008 10:31 AM
    Tuesday, August 19, 2008 1:31 PM
  • Set the focus scope to the layout that would contain the focusable control - FocusManager.SetIsFocusScope(mainGrid, true)

    Get the focused control using -  var focusedElement = FocusManager.GetFocusedElement(mainGrid);

    Friday, November 12, 2010 12:12 PM