locked
WPF Control focus issue. RRS feed

  • Question

  • Hi all, can i get the previous focused contrl from a WPF form or control when focus switch to another WPF control.
    Wednesday, February 17, 2010 9:06 AM

Answers

  • You could override OnPreviewLostKeyboardFocus on your window, which will tell you what both the new and the old focused items are. However, for some more complex controls this might not produce the results you expect. E.g. when changing focus to an editable combo box you can briefly see the combo box getting then losing focus, and the focus is ultimately switched to the combo box's internal text box. Perhaps you can elaborate on what it is you're trying to achieve?
    • Marked as answer by Vinod.meee Thursday, February 18, 2010 7:17 AM
    Wednesday, February 17, 2010 12:03 PM

All replies

  • You could override OnPreviewLostKeyboardFocus on your window, which will tell you what both the new and the old focused items are. However, for some more complex controls this might not produce the results you expect. E.g. when changing focus to an editable combo box you can briefly see the combo box getting then losing focus, and the focus is ultimately switched to the combo box's internal text box. Perhaps you can elaborate on what it is you're trying to achieve?
    • Marked as answer by Vinod.meee Thursday, February 18, 2010 7:17 AM
    Wednesday, February 17, 2010 12:03 PM
  • Hi Vinod.meee,
    As far as I know, there is no build-in method can get the previous element which got focus. but there is a way to predict the next element that would receive focus relative to the current element by calling PredictFocus method. We provide a focus movement direction as the method parameter.

    Thanks.
    Sincerely.

    Jim Zhou -MSFT
    Wednesday, February 17, 2010 12:44 PM
  • Thank you verymuch dekurver, i am new to WPF and this is really a usefull infomration for me :-)
    Thursday, February 18, 2010 7:17 AM
  • Thanks Jim this is also new information for me :-)

    Thursday, February 18, 2010 7:18 AM
  • Hi Vinod.meee,

    You are welcome. If you are still having any issues about WPF, please feel free to feed back.

    Thanks.
    Sincerely.
    Jim Zhou -MSFT
    Tuesday, February 23, 2010 11:41 AM