Set focus on a textbox control in UserControl in wpf

前端 未结 7 874
孤独总比滥情好
孤独总比滥情好 2020-12-05 07:46

I have created an UserControl which is loaded in a View (Window) in WPF. In my user control I have put a TextBox. I am unable to set focus on this

7条回答
  •  自闭症患者
    2020-12-05 08:28

    What i use in my authentication manager:

    private void SelectLogicalControl()
    {
        if (string.IsNullOrEmpty(TextboxUsername.Text))
            TextboxUsername.Focus();
        else
        {
            TextboxPassword.SelectAll();
            TextboxPassword.Focus();
        }
    }
    

    If no username is set, focus on the username-textbox; otherwise the (select all) passwordbox. This is in the codebehind-file, so not viewmodel ;)

提交回复
热议问题