WPF:自动完成文本框,…再次

这个其他SO问题关于WPF中的自动完成文本框。 有几个人已经build立了这些,并给出了答案之一,build议这个codeproject文章 。

但我还没有find任何WPF自动完成文本框与WinForms自动完成文本框进行比较。 代码项目示例工程,sorting,…

替代文字http://i50.tinypic.com/sx2ej5.jpg

…但

  • 它不被构造成可重用的控件或DLL。 这是我需要embedded到每个应用程序的代码。
  • 它只适用于目录。 它没有用于设置自动完成源仅为文件系统目录还是文件系统文件或….等的属性。 当然,我可以编写代码来做到这一点,但是…我宁愿使用别人已经编写的代码。
  • 它没有属性来设置popup窗口大小等
  • 有一个popup列表框,提供可能的完成。 浏览该列表时,文本框不会更改。 在列表框中键入字符并不会导致文本框被更新。
  • 从列表框导航焦点不会使popup式列表框消失。 这很混乱。

所以,我的问题是:

*有没有人有一个免费的WPF自动完成文本框工作 ,并提供一个高品质的用户体验?


回答

以下是我如何做到的:

0.0。 获得WPF工具包

0.1。 运行WPF工具包的MSI

0.2。 在Visual Studio中,从工具箱(特别是数据可视化组)拖放到UI Designer中。 它在VS工具箱中看起来像这样:

替代文字160wpb4.jpg

如果你不想使用devise师,手工制作xaml。 它看起来像这样:


<toolkit:AutoCompleteBox ToolTip="Enter the path of an assembly." x:Name="tbAssembly" Height="27" Width="102" Populating="tbAssembly_Populating" /> 

…工具箱命名空间以这种方式映射的地方:

 xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit" 

0.3。 提供Populating事件的代码。 这是我用的:


 private void tbAssembly_Populating(object sender, System.Windows.Controls.PopulatingEventArgs e) { string text = tbAssembly.Text; string dirname = Path.GetDirectoryName(text); if (Directory.Exists(Path.GetDirectoryName(dirname))) { string[] files = Directory.GetFiles(dirname, "*.*", SearchOption.TopDirectoryOnly); string[] dirs = Directory.GetDirectories(dirname, "*.*", SearchOption.TopDirectoryOnly); var candidates = new List<string>(); Array.ForEach(new String[][] { files, dirs }, (x) => Array.ForEach(x, (y) => { if (y.StartsWith(dirname, StringComparison.CurrentCultureIgnoreCase)) candidates.Add(y); })); tbAssembly.ItemsSource = candidates; tbAssembly.PopulateComplete(); } } 

它的工作,就像你所期望的那样。 感觉专业。 代码项目控制不存在任何exception情况。 这是它的样子:

替代文字http://i50.tinypic.com/24qsopy.jpg


感谢马特指向 WPF工具包的指针 。

WPF工具包的最新版本包括一个AutoCompleteBox。 这是来自Microsoft的一套免费控件,其中一些将包含在.NET 4中。

杰夫·威尔考克斯 – 介绍AutoCompleteBox

以下是我如何做到的:

0.1。 运行WPF工具包的MSI

0.2。 在Visual Studio中,从工具箱(特别是数据可视化组)拖放到UI Designer中。 它在VS工具箱中看起来像这样:

替代文字160wpb4.jpg

或者,手工制作xaml。 它看起来像这样:


 <toolkit:AutoCompleteBox ToolTip="Enter the path of an assembly." x:Name="tbAssembly" Height="27" Width="102" Populating="tbAssembly_Populating" /> 

…工具箱命名空间以这种方式映射的地方:

 xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit" 

0.3。 提供Populating事件的代码。 这是我用的:


 private void tbAssembly_Populating(object sender, System.Windows.Controls.PopulatingEventArgs e) { string text = tbAssembly.Text; string dirname = Path.GetDirectoryName(text); if (Directory.Exists(Path.GetDirectoryName(dirname))) { string[] files = Directory.GetFiles(dirname, "*.*", SearchOption.TopDirectoryOnly); string[] dirs = Directory.GetDirectories(dirname, "*.*", SearchOption.TopDirectoryOnly); var candidates = new List<string>(); Array.ForEach(new String[][] { files, dirs }, (x) => Array.ForEach(x, (y) => { if (y.StartsWith(dirname, StringComparison.CurrentCultureIgnoreCase)) candidates.Add(y); })); tbAssembly.ItemsSource = candidates; tbAssembly.PopulateComplete(); } } 

感谢马特指向WPF工具包的指针。

Mindscape还提供了3个免费的控件,包括一个WPF自动完成文本框

http://intellibox.codeplex.com/似乎最近在2013年10月1日更新,并包含单个控件。; 我会补充说,特洛伊的答案,但没有足够的代表。 由于这个评论我几乎没有理会。

来自文档的示例用法:

  <auto:Intellibox ResultsHeight="80" ExplicitlyIncludeColumns="True" Name="lightspeedBox" DisplayedValueBinding="{Binding Product_Name}" SelectedValueBinding="{Binding Product_Id}" DataProvider="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=LinqToEntitiesProvider}" Height="26" Margin="12,26,12,0" VerticalAlignment="Top"> <auto:Intellibox.Columns> <auto:IntelliboxColumn DisplayMemberBinding="{Binding Product_Name}" Width="150" Header="Product Name" /> <auto:IntelliboxColumn DisplayMemberBinding="{Binding Unit_Price}" Width="75" Header="Unit Price" /> <auto:IntelliboxColumn DisplayMemberBinding="{Binding Suppliers.Company_Name}" Width="125" Header="Supplier" /> </auto:Intellibox.Columns> </auto:Intellibox> 

我在我的内部项目中使用Intellibox。 http://intellibox.codeplex.com/

我发现它使用Provider模式进行search非常直观。

Rake的答案提供了一个如何使用它的例子,正如他指出的那样,去年年底已经有了一些发展(虽然这是在我上次使用它之后)。

您可以在CodePlex上尝试WPF自动完成文本框: https : //wpfautocomplete.codeplex.com/