简体   繁体   English

在UserControl内设置TextBox的焦点

[英]Set Focus of TextBox inside UserControl

I have a UserControl with just a TextBox inside it, this TextBox has some attached properties and some code behind, since I am going to use it in multiple Views, I created a UserControl with it to improve reusability and maintenance. 我有一个UserControl其中只有一个TextBox ,该TextBox具有一些附加的属性和一些代码,由于我打算在多个视图中使用它,因此我创建了一个UserControl以提高可重用性和维护性。

This TextBox is used for Search, so I trying to set it on Focus when the user makes a Ctrl+F , this works fine, the problem is, I can't make the TextBox focus, just the UserControl gets focused. TextBox用于搜索,因此我尝试在用户进行Ctrl+F时将其设置为Focus,这很好用,问题是,我无法使TextBox焦点,只是UserControl获得了焦点。

So the question is: How to redirect the Focus of the UserControl to the TextBox inside it? 所以问题是:如何将UserControlFocus重定向到其中的TextBox

I tried already: 我已经尝试过了:

protected override void OnPreviewGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
    this.TextBox.SelectAll();        
    base.OnPreviewGotKeyboardFocus(e);
}

and many other different combinations, like changing the order the base is called, or removing it...calling this.TextBox.Focus() which sets a StackOverFlow...verifying if this.TextBox.IsFocused doesn't work, it's always false ...so... how can I achieve this? 以及其他许多不同的组合,例如更改base的调用顺序或将其删除...调用this.TextBox.Focus()设置StackOverFlow ...验证this.TextBox.IsFocused是否无效,始终false ...所以...我该如何实现?

Ok, got it, I changed the: 好的,知道了,我更改了:

protected override void OnPreviewGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
    this.TextBox.SelectAll();        
    base.OnPreviewGotKeyboardFocus(e);
}

to: 至:

protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
    this.TextBox.SelectAll();        
    base.OnGotKeyboardFocus(e);
}

I guess the whole RoutedEvents interactions of bubbling and tunneling was interfering. 我猜冒泡和隧道RoutedEvents的整个RoutedEvents交互都是干扰的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM