【问题标题】:autocompletebox focus in wpfwpf中的自动完成框焦点
【发布时间】:2011-04-04 02:12:13
【问题描述】:

当我尝试专注于我的“自动完成文本框”时,我失败了,我写了autocompletetextbox.focus() 但是光标仍然聚焦在另一个我应该做什么或写什么才能在其中写或聚焦?

【问题讨论】:

    标签: c# wpf autocomplete


    【解决方案1】:

    我也遇到过同样的事情——它在当前形式下无法正常工作(我希望您说的是 2010 年 2 月发布的 WPFToolkit 附带的 AutoCompleteBox)。

    我创建了一个子类:

    public class AutoCompleteFocusableBox : AutoCompleteBox
    {
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            var textbox = Template.FindName("Text", this) as TextBox;
            if(textbox != null) textbox.Focus();
        }
    }
    

    这会将焦点设置为实际的TextBox(称为“文本”),它是默认ControlTemplate 的一部分。

    【讨论】:

    • 我应该把这门课放在哪里?
    • 在您的 XAML 中,您使用它而不是 AutoCompleteBox。因此,如果您在命名空间 XYZ 中有此类,则将该命名空间导入 XAML 文件的根目录,并使用 xyz 之类的别名,然后使用 跨度>
    • 我试过了,但是 blend 给我一个错误 AutoCompleteFocusableBox 不支持 wpf!!!
    • 您实际上使用的是 2010 年 2 月发布的 WPFToolkit 吗?什么版本的混合?这在 Blend 3 中对我有用。在 Blend 中加载您的项目后,您应该能够使用工具箱中的搜索功能找到此控件,并像任何其他控件一样通过 GUI 添加它。
    • @Jay 该类应该使用什么设置?我只有一个名为 BetterACBox 的 .cs 文件,并且在我的 xaml 窗口中导入了命名空间,但是在添加控件时出现以下错误:the specified value cannot be assigned to the collection. the following type was expected uielement
    【解决方案2】:

    您必须重写 Focus 方法才能找到文本框的模板。

    public class FocusableAutoCompleteBox : AutoCompleteBox
    {
        public new void Focus()
        {
            var textbox = Template.FindName("Text", this) as TextBox;
            if (textbox != null) textbox.Focus();
        }
    }
    

    【讨论】:

    • using System.Windows.Controls;
    • 以及如何在方法中调用它?
    【解决方案3】:

    这是一个很老的问题,但我想分享一下我的解决方法。

    Keyboard.Focus(autocompletetextbox);
    autocompletetextbox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
    

    这适用于WPFToolkit v3.5.50211.1Visual Studio Express 2015 for Windows Desktop

    【讨论】:

      【解决方案4】:

      您似乎必须先等待自动完成框加载。然后设置焦点

      <sdk:AutoCompleteBox 
         x:Name="_employeesAutoCompleteBox" 
         ItemsSource="{Binding Path=Employees}" 
         SelectedItem="{Binding SelectedEmployee, Mode=TwoWay}" 
         ValueMemberPath="DisplayName" >    
      </sdk:AutoCompleteBox>
      
      _employeesAutoCompleteBox.Loaded +=
          (sender, e) => ((AutoCompleteBox)sender).Focus();
      

      【讨论】:

        【解决方案5】:

        这是我的解决方案,

        我发现它比继承类更容易

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            var textBox = FindVisualChild<TextBox>(CodedCommentBox);
            textBox.Focus();
        }
        
        private TChildItem FindVisualChild<TChildItem>(DependencyObject obj) where TChildItem : DependencyObject
        {
            for (var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                var child = VisualTreeHelper.GetChild(obj, i);
        
                var item = child as TChildItem;
                if (item != null)
                {
                    return item;
                }
        
                var childOfChild = FindVisualChild<TChildItem>(child);
                if (childOfChild != null)
                {
                    return childOfChild;
                }
            }
        
            return null;
        }
        

        【讨论】:

          【解决方案6】:

          这是我将焦点设置在 AutoCompleteTextBox 控件文本上的解决方案:

          private void MyPageLoaded(object sender, RoutedEventArgs e) {

          var myPage = (MyControl)sender;
          var autoTextBox = (AutoCompleteTextBox)myPage.FindName("AutoTextBox");
          
          if (autoTextBox != null)
          {
              var innerTextBox = autoTextBox.textBox;
              if (innerTextBox != null)
              {
                  innerTextBox.Focus();
          
              }
          }
          

          }

          【讨论】:

            【解决方案7】:

            您也可以为此使用扩展方法:

            public static void ForceFocus(this AutoCompleteBox box)
            {
                if (box.Template.FindName("Text", box) is TextBox textbox)
                {
                    textbox.Focus();
                }
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2010-10-31
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-03-13
              相关资源
              最近更新 更多