5

XAML C# not WEB page is a Window

At the click of a button I:

  1. Need to trap the name of the last control OnFocus
  2. Force the LostFocus event of the control.
Alex
  • 1,549
  • 2
  • 16
  • 41
ramnz
  • 631
  • 1
  • 6
  • 24

2 Answers2

4

// PROBLEM: clicking on btns does not force lostfocus event on the last entered element control (last entry control could be text,checkbox or others) added save button, where calling such method it moves focus to parent forcing lostfocus on last element.

    private void btnSave_Click(object sender, RoutedEventArgs e)
    {
        AcceptLastFocusedElement(sender, e);
    }


    private void AcceptLastFocusedElement(object sender, RoutedEventArgs e)
    {
        FocusManager.SetFocusedElement(this, (Button)sender);
    }

NOTE: no need for task number 1 (getting the name of the element).

ramnz
  • 631
  • 1
  • 6
  • 24
  • This works... but not alway. I had a case where the second time we ran this code, it dis not cause a lostfocus because of FocusGroup. The solution in this answer http://stackoverflow.com/a/4724766/197371 solved my specific problem – Marcel Gosselin Jul 25 '12 at 14:58
  • am glad that we narrow out these issues!. – ramnz Jul 26 '12 at 21:11
2

You could make use of the LayoutUpdated method.

So whenever there is any event happening, it goes into the LayoutUpdatedevent and you could trap the LastFocusObject .

Alex
  • 1,549
  • 2
  • 16
  • 41
Bajju
  • 925
  • 11
  • 27