我创建了一个 UserControl
它在 WPF 的 View (窗口)中加载。在我的用户控件中,我放了一个 TextBox
.当我的 View 加载时,我无法将焦点设置在此文本框上。我试过以下但对我没有任何作用:
FocusManager.FocusedElement="{Binding ElementName=PwdBox}"
FocusExtension
将重点放在控制上。 请帮忙。
最佳答案
注册Loaded-Event您的 UserControl 并通过调用 Focus() 将焦点设置在您的 PwdBox 上当你的 UserControl 被加载时。
public class MyUserControl : UserControl{
public MyUserControl(){
this.Loaded += Loaded;
}
public void Loaded(object sender, RoutedEventArgs e){
PwdBox.Focus();
// or FocusManager.FocusedElement = PwdBox;
}
}
关于wpf - 在 wpf 中的 UserControl 中将焦点设置在文本框控件上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19225443/