Can't set focus to a child of UserControl

后端 未结 18 464
耶瑟儿~
耶瑟儿~ 2020-12-04 21:55

I have a UserControl which contains a TextBox. When my main window loads I want to set the focus to this textbox so I added Focusable=\"True

相关标签:
18条回答
  • 2020-12-04 22:19
    1. Set your user control to Focusable="True" (XAML)
    2. Handle the GotFocus event on your control and call yourTextBox.Focus()
    3. Handle the Loaded event on your window and call yourControl.Focus()

    I have a sample app running with this solution as I type. If this does not work for you, there must be something specific to your app or environment that causes the problem. In your original question, I think the binding is causing the problem. I hope this helps.

    0 讨论(0)
  • 2020-12-04 22:20

    What did the trick for me was the FocusManager.FocusedElement attribute. I first tried to set it on the UserControl, but it didn't work.

    So I tried putting it on the UserControl's first child instead:

    <UserControl x:Class="WpfApplication3.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid FocusManager.FocusedElement="{Binding ElementName=MyTextBox, Mode=OneWay}">
        <TextBox x:Name="MyTextBox"/>
    </Grid>
    

    ... and it worked! :)

    0 讨论(0)
  • 2020-12-04 22:24

    Assuming you want to set focus for Username textbox, thus user can type in directly every time it shows up.

    In Constructor of your control:

    this.Loaded += (sender, e) => Keyboard.Focus(txtUsername);
    
    0 讨论(0)
  • 2020-12-04 22:25

    I don't like solutions with setting another tab scope for UserControl. In that case, you will have two different carets when navigating by keyboard: on the window and the another - inside user control. My solution is simply to redirect focus from user control to inner child control. Set user control focusable (because by default its false):

    <UserControl ..... Focusable="True">
    

    and override focus events handlers in code-behind:

    protected override void OnGotFocus(RoutedEventArgs e)
    {
        base.OnGotFocus(e);
        MyTextBox.Focus();
    }
    
    protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
    {
        base.OnGotKeyboardFocus(e);
        Keyboard.Focus(MyTextBox);
    }
    
    0 讨论(0)
  • 2020-12-04 22:26

    Its stupid but it works:

    Pop a thread that waits a while then comes back and sets the focus you want. It even works within the context of an element host.

    private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    
     System.Threading.ThreadPool.QueueUserWorkItem(
                       (a) =>
                            {
                                System.Threading.Thread.Sleep(100);
                                someUiElementThatWantsFocus.Dispatcher.Invoke(
                                new Action(() =>
                                {
                                    someUiElementThatWantsFocus.Focus();
    
                                }));
                            }
                       );
    
    }
    
    0 讨论(0)
  • 2020-12-04 22:30

    I've noticed a focus issue specifically related to hosting WPF UserControls within ElementHosts which are contained within a Form that is set as an MDI child via the MdiParent property.

    I'm not sure if this is the same issue others are experiencing but you dig into the details by following the link below.

    Issue with setting focus within a WPF UserControl hosted within an ElementHost in a WindowsForms child MDI form

    0 讨论(0)
提交回复
热议问题