locked
running into a loop on LostFocus event handled and usercontrol.OnPreviewGotKeyboardFocus event handled RRS feed

  • Question

  • I have user control having a textbox and few other controls. I want to focus the textbox when usercontrol got focus on tab changes and can be assigned Alt shortkey. For this I used:
    <UserControl x:Class="CustomControlProject.UserControl1" xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
    xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Height="Auto" Width="Auto" PreviewGotKeyboardFocus="UserControl_PreviewGotKeyboardFocus" Focusable="True" >
        <Grid>
            <TextBox x:Name="txt" />
        </Grid>
    </UserControl>

    Code behind of user control, I have created a public property and handled the PreviewGotKeyboardFocus event.
            public string Text
            {
                get { return txt.Text; }
                set { txt.Text = value; }
            }
            private void UserControl_PreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
            {
                if (e.NewFocus == this)
                {
                    txt.Focus();
                }
            }


    In my WPF application I m using this usercontrol

    <

     

    CustomControl:UserControl1 x:Name="usrControl" />

    Code behind of WPF application page:

        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
                usrControl.LostFocus +=new RoutedEventHandler(usrControl_LostFocus);
            }
            void usrControl_LostFocus(object sender, RoutedEventArgs e)
            {
                if (usrControl.Text == "")
                {
                    usrControl.Focus();
                }
            }
        }


    When I run this, I get the System.StackOverflowException. As the LostFocus event handler and PreviewGotKeyBoardFocus handler forms a loop. Please assist me on this.

    Thursday, April 23, 2009 4:13 PM

Answers

  • The issue is that when you set the focus on txt, your usercontrol loses focus, which then causes you to set focus on the usercontrol, causing the user control to set focus on txt causing the user control to lose focus...
    What you want to do is swallow the LostFocus event if it's a result of setting the focus on your text box. This code should help:
    public UserControl1()
    {
      InitializeComponent();
      LostFocus+=HandleLostFocus;
      PreviewGotKeyboardFocus+=
         UserControl_PreviewGotKeyboardFocus;
    }
    
    private bool _IsSettingFocus=false;
    
    private void UserControl_PreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
            {
                if (e.NewFocus == this)
                {
                    _IsSettingFocus=true;
                    txt.Focus();
                }
            }
    void HandleLostFocus(object sender, RoutedEventArgs e)
    {
       if(_IsSettingFocus)
         e.Handled=true;
       _IsSettingFocus=false;
    }
    
    


    Keep your head in the Clouds as you're coding .NET http://azurecoding.net
    • Edited by Brownie Points Thursday, April 23, 2009 5:35 PM fixed a typo
    • Proposed as answer by Tao Liang Wednesday, April 29, 2009 3:28 AM
    • Marked as answer by Tao Liang Thursday, April 30, 2009 3:49 AM
    Thursday, April 23, 2009 5:34 PM

All replies

  • I don't understand why you need to handle the LostFocus event?
    Thursday, April 23, 2009 4:40 PM
  • The issue is that when you set the focus on txt, your usercontrol loses focus, which then causes you to set focus on the usercontrol, causing the user control to set focus on txt causing the user control to lose focus...
    What you want to do is swallow the LostFocus event if it's a result of setting the focus on your text box. This code should help:
    public UserControl1()
    {
      InitializeComponent();
      LostFocus+=HandleLostFocus;
      PreviewGotKeyboardFocus+=
         UserControl_PreviewGotKeyboardFocus;
    }
    
    private bool _IsSettingFocus=false;
    
    private void UserControl_PreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
            {
                if (e.NewFocus == this)
                {
                    _IsSettingFocus=true;
                    txt.Focus();
                }
            }
    void HandleLostFocus(object sender, RoutedEventArgs e)
    {
       if(_IsSettingFocus)
         e.Handled=true;
       _IsSettingFocus=false;
    }
    
    


    Keep your head in the Clouds as you're coding .NET http://azurecoding.net
    • Edited by Brownie Points Thursday, April 23, 2009 5:35 PM fixed a typo
    • Proposed as answer by Tao Liang Wednesday, April 29, 2009 3:28 AM
    • Marked as answer by Tao Liang Thursday, April 30, 2009 3:49 AM
    Thursday, April 23, 2009 5:34 PM