none
WPF usercontrol lostfocus event

    Question

  • hi,

     

    This is the problem related to WPF usercontrol lostfocus event. I have a usercontrol NumericUpDownControl containing one textbox and two buttons. I have to run some logic on lostfocus event of the usercontrol. But what i found is, the lostfocus event of the usercontrol gets fired even when i move within the usercontrol (means i am changing the focus from the textbox to the button of the usercontrol.). So everytime i change the focus within the usercontrol, the lostfocus event of the usercontrol gets fired.

    i want the lostfocus to get fire only when i move out of the usercontrol.

    how can i ensure that the when the lostfocus is fired (1. while changing the focus within the usercontrol. OR 2. changing the focus from the usercotrol to some other control.)

     

    I found following explanation in the MSDN documentation -

     

    Because this event uses bubbling routing, the element that loses focus might be a child element instead of the element where the event handler is actually attached. (http://msdn.microsoft.com/en-us/library/system.windows.uielement.lostfocus.aspx)

     

    can anybody suggest what can be done in this scenario.

     

     

    Thanks in advance

     

    ~Sachin

     

     

     

    Monday, May 26, 2008 8:44 AM

Answers

  • I believe what you need is this:

    <StackPanel x:Name="container">
      <UserControl x:Name="userControl" Background="Green" Width="300" Height="150">
        <StackPanel>
          <TextBox Width="120" Height="30" Margin="5"/>
          <TextBox Width="120" Height="30" Margin="5"/>
        </StackPanel>
      </UserControl>
      <TextBox Width="120" Height="30" Text="Catch Focus"/>
    </StackPanel>

    container.GotFocus += (sender, e) =>
    {
        if (e.Source != userControl)
        {
            // Here we know that the UserControl along with its child has lost focus.
            MessageBox.Show("UserControl lost focs");
        }
    };

    You could examine the UserControl.IsKeyboardFocusWithin to determine if the UserControl and its children has got keyboard focus or not.

    Hope this helps
    • Marked as answer by Marco Zhou Monday, June 02, 2008 2:18 AM
    Thursday, May 29, 2008 4:51 AM

All replies

  • I believe what you need is this:

    <StackPanel x:Name="container">
      <UserControl x:Name="userControl" Background="Green" Width="300" Height="150">
        <StackPanel>
          <TextBox Width="120" Height="30" Margin="5"/>
          <TextBox Width="120" Height="30" Margin="5"/>
        </StackPanel>
      </UserControl>
      <TextBox Width="120" Height="30" Text="Catch Focus"/>
    </StackPanel>

    container.GotFocus += (sender, e) =>
    {
        if (e.Source != userControl)
        {
            // Here we know that the UserControl along with its child has lost focus.
            MessageBox.Show("UserControl lost focs");
        }
    };

    You could examine the UserControl.IsKeyboardFocusWithin to determine if the UserControl and its children has got keyboard focus or not.

    Hope this helps
    • Marked as answer by Marco Zhou Monday, June 02, 2008 2:18 AM
    Thursday, May 29, 2008 4:51 AM
  • Hi!

    I have the same problem, my user control consists of a textbox and a popup control containing a listview. The problem for me is that I cant use the above proposed solution since I dont have access to the xaml using the user control.

    I need my user control to take certain actions whenever the focus has left both the textbox and the list view and unfortunately the IsKeyBoardFocusWithin does not work as I need it to for some reason.

    Is there any way of doing this? If there is a way to find out wich control will get the focus I can get what I need?

    Thanks for any help!
    ::pelle
    Wednesday, March 04, 2009 11:04 AM