WPF 中的自动完成文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/950770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:31:33  来源:igfitidea点击:

AutoComplete TextBox in WPF

wpftextboxautocompletewpf-controls

提问by

Is it possible to make a textbox autocomplete in WPF?

是否可以在 WPF 中使文本框自动完成?

I found a sample where a combo box is used and the triangle is removed by editing the style template.

我找到了一个示例,其中使用了组合框,并通过编辑样式模板删除了三角形。

Is there a better solution?

有更好的解决方案吗?

回答by Alexander Zwitbaum

You can find one in the WPF Toolkit, which is also available via NuGet.

您可以在WPF Toolkit 中找到一个,也可以通过 NuGet 获得。

This article demos how to create a textbox which can auto-suggest items at runtime based on input, in this case, disk drive folders. WPF AutoComplete Folder TextBox

本文演示了如何创建一个文本框,该文本框可以在运行时根据输入(在本例中为磁盘驱动器文件夹)自动建议项目。 WPF 自动完成文件夹文本框

Also take a look at this nice Reusable WPF Autocomplete TextBox, it was for me very usable.

也看看这个不错的Reusable WPF Autocomplete TextBox,它对我来说非常有用。

回答by JumpingJezza

Nimgoble'sis the version I used in 2015. Thought I'd put it here as this question was top of the list in google for "wpf autocomplete textbox"

Nimgoble是我在 2015 年使用的版本。我想我会把它放在这里,因为这个问题是谷歌“wpf 自动完成文本框”列表的顶部

  1. Install nuget package for project in Visual Studio

  2. Add a reference to the library in the xaml:
    xmlns:behaviors="clr-namespace:WPFTextBoxAutoComplete;assembly=WPFTextBoxAutoComplete"

  3. Create a textbox and bind the AutoCompleteBehaviour to List<String>(TestItems):
    <TextBox Text="{Binding TestText, UpdateSourceTrigger=PropertyChanged}"behaviors:AutoCompleteBehavior.AutoCompleteItemsSource="{Binding TestItems}" />

  1. 在 Visual Studio 中为项目安装 nuget 包

  2. 在 xaml 中添加对库的引用:
    xmlns:behaviors="clr-namespace:WPFTextBoxAutoComplete;assembly=WPFTextBoxAutoComplete"

  3. 创建一个文本框并将 AutoCompleteBehaviour 绑定到List<String>(TestItems):
    <TextBox Text="{Binding TestText, UpdateSourceTrigger=PropertyChanged}"behaviors:AutoCompleteBehavior.AutoCompleteItemsSource="{Binding TestItems}" />

IMHO this is much easier to get started and manage than the other options listed above.

恕我直言,这比上面列出的其他选项更容易开始和管理。

回答by Deepak Bhardwaj

I have published a WPF Auto Complete Text Box in WPF at CodePlex.com. You can download and try it from https://wpfautocomplete.codeplex.com/.

我在 CodePlex.com 上发布了 WPF 中的 WPF 自动完成文本框。您可以从https://wpfautocomplete.codeplex.com/下载并试用它。

回答by ARidder101

I know this is a very old question but I want to add an answer I have come up with.

我知道这是一个非常古老的问题,但我想添加一个我想出的答案。

First you need a handler for your normal TextChangedevent handler for the TextBox:

首先,您需要为以下的正常TextChanged事件处理程序提供处理程序TextBox

private bool InProg;
internal void TBTextChanged(object sender, TextChangedEventArgs e)
            {
            var change = e.Changes.FirstOrDefault();
            if ( !InProg )
                {
                InProg = true;
                var culture = new CultureInfo(CultureInfo.CurrentCulture.Name);
                var source = ( (TextBox)sender );
                    if ( ( ( change.AddedLength - change.RemovedLength ) > 0 || source.Text.Length > 0 ) && !DelKeyPressed )
                        {
                         if ( Files.Where(x => x.IndexOf(source.Text, StringComparison.CurrentCultureIgnoreCase) == 0 ).Count() > 0 )
                            {
                            var _appendtxt = Files.FirstOrDefault(ap => ( culture.CompareInfo.IndexOf(ap, source.Text, CompareOptions.IgnoreCase) == 0 ));
                            _appendtxt = _appendtxt.Remove(0, change.Offset + 1);
                            source.Text += _appendtxt;
                            source.SelectionStart = change.Offset + 1;
                            source.SelectionLength = source.Text.Length;
                            }
                        }
                InProg = false;
                }
            }

Then make a simple PreviewKeyDownhandler:

然后做一个简单的PreviewKeyDown处理程序:

    private static bool DelKeyPressed;
    internal static void DelPressed(object sender, KeyEventArgs e)
    { if ( e.Key == Key.Back ) { DelKeyPressed = true; } else { DelKeyPressed = false; } }

In this example "Files" is a list of directory names created on application startup.

在此示例中,“文件”是在应用程序启动时创建的目录名称列表。

Then just attach the handlers:

然后只需附加处理程序:

public class YourClass
  {
  public YourClass()
    {
    YourTextbox.PreviewKeyDown += DelPressed;
    YourTextbox.TextChanged += TBTextChanged;
    }
  }

With this whatever you choose to put in the Listwill be used for the autocomplete box. This may not be a great option if you expect to have an enormous list for the autocomplete but in my app it only ever sees 20-50 items so it cycles through very quick.

有了这个,您选择放入的任何内容List都将用于自动完成框。如果您希望有一个庞大的自动完成列表,这可能不是一个很好的选择,但在我的应用程序中,它只能看到 20-50 个项目,因此循环非常快。

回答by MelloG

or you can add the AutoCompleteBox into the toolbox by clicking on it and then Choose Items, go to WPF Components, type in the filter AutoCompleteBox, which is on the System.Windows.Controls namespace and the just drag into your xaml file. This is way much easier than doing these other stuff, since the AutoCompleteBox is a native control.

或者您可以通过单击 AutoCompleteBox 将它添加到工具箱中,然后选择项目,转到 WPF 组件,输入过滤器 AutoCompleteBox,它位于 System.Windows.Controls 命名空间中,然后将其拖到您的 xaml 文件中。这比做其他这些事情要容易得多,因为 AutoCompleteBox 是一个本机控件。

回答by user2475096

and here the fork of the toolkit wich contains the port to 4.O,

在这里工具包的分支包含到 4.O 的端口,

https://github.com/jogibear9988/wpftoolkit

https://github.com/jogibear9988/wpftoolkit

it's worked very well to me .

它对我很有效。

回答by Jeson Martajaya

If you have a small number of values to auto complete, you can simply add them in xaml. Typing will invoke auto-complete, plus you have dropdowns too.

如果您有少量值要自动完成,您只需将它们添加到 xaml 中即可。键入将调用自动完成功能,此外您还有下拉菜单。

<ComboBox Text="{Binding CheckSeconds, UpdateSourceTrigger=PropertyChanged}"
          IsEditable="True">
    <ComboBoxItem Content="60"/>
    <ComboBoxItem Content="120"/>
    <ComboBoxItem Content="180"/>
    <ComboBoxItem Content="300"/>
    <ComboBoxItem Content="900"/>
</ComboBox>