Set focus on textbox in WPF

后端 未结 9 809
-上瘾入骨i
-上瘾入骨i 2020-11-28 08:03

How to set the focus on an TextBox element in WPF

I have this code:

txtCompanyID.Focusable = true;
txtCompanyID.Focus();
相关标签:
9条回答
  • 2020-11-28 08:18

    In case you haven't found the solution on the other answers, that's how I solved the issue.

    Application.Current.Dispatcher.BeginInvoke(new Action(() =>
    {
        TEXTBOX_OBJECT.Focus();
    }), System.Windows.Threading.DispatcherPriority.Render);
    

    From what I understand the other solutions may not work because the call to Focus() is invoked before the application has rendered the other components.

    0 讨论(0)
  • 2020-11-28 08:22

    Another possible solution is to use FocusBehavior provided by free DevExpress MVVM Framework:

    <TextBox Text="This control is focused on startup">
        <dxmvvm:Interaction.Behaviors>
            <dxmvvm:FocusBehavior/>
        </dxmvvm:Interaction.Behaviors>
    </TextBox>
    

    It allows you to focus a control when it's loaded, when a certain event is raised or a property is changed.

    0 讨论(0)
  • 2020-11-28 08:30

    Try this : MyTextBox.Focus ( );

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