问题描述
...但是
- 它不是可重复使用的控件或 DLL.这是我需要嵌入到每个应用程序中的代码.
- 它仅适用于目录.它没有用于设置自动完成源是仅文件系统目录还是文件系统文件或...等的属性.当然,我可以编写代码来执行此操作,但是……我宁愿使用其他人已经编写的代码.
- 它没有设置弹出窗口大小等的属性
- 有一个弹出列表框,显示可能的完成.在该列表中导航时,文本框不会改变.在列表框中输入字符不会导致文本框更新.
- 将焦点从列表框移开不会使弹出列表框消失.这令人困惑.
所以,我的问题是:
*有人有免费的 WPF 自动完成文本框有效,并提供优质的 UI 体验吗?*
答案
我是这样做的:
.0.获取
如果您不想使用设计器,请手工制作 xaml.它看起来像这样:
...工具包命名空间以这种方式映射:
xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
.3.为 Populating
事件提供代码.这是我使用的:
private void tbAssembly_Populating(对象发送者,System.Windows.Controls.PopulatingEventArgs e){字符串文本 = tbAssembly.Text;字符串目录名 = Path.GetDirectoryName(text);if (Directory.Exists(Path.GetDirectoryName(dirname))){string[] files = Directory.GetFiles(dirname, "*.*", SearchOption.TopDirectoryOnly);string[] dirs = Directory.GetDirectories(dirname, "*.*", SearchOption.TopDirectoryOnly);varCandidate = new List();Array.ForEach(new String[][] { files, dirs }, (x) =>Array.ForEach(x, (y) =>{if (y.StartsWith(dirname, StringComparison.CurrentCultureIgnoreCase))候选人.Add(y);}));tbAssembly.ItemsSource = 候选人;tbAssembly.PopulateComplete();}}
它的工作原理与您期望的一样.感觉很专业.codeproject 控件没有任何异常.这是它的样子:
WPF 的最新版本Toolkit 包括一个 AutoCompleteBox.这是 Microsoft 提供的一组免费控件,其中一些将包含在 .NET 4 中.
Jeff Wilcox - 介绍 AutoCompleteBox
This other SO question asks about an autocomplete textbox in WPF. Several people have built these, and one of the answers given there suggests this codeproject article.
But I've not found any WPF Autocomplete Textbox that compares with the WinForms autocomplete textbox. The codeproject sample works, sort of, ...
...but
- it isn't structured as a re-usable control or DLL. It's code I need to embed in every app.
- It works only with directories. it doesn't have properties for setting whether the autocomplete source is filesystem directories only, or filesystem files, or ....etc. I could write code to do this, of course, but...I'd rather use someone else's code already written.
- it doesn't have properties to set the popup size, etc.
- there's a popup listbox that presents the posible completions. When navigating through that list, the textbox doesn't change. Typing a character while focused in the listbox doesn't cause the textbox to get updated.
- navigating focus away from the listbox doesn't make the popup listbox disappear. This is confusing.
So, my question:
*Does anyone have a FREE WPF AutoComplete textbox that works, and provides a quality UI experience?*
ANSWER
Here's how I did it:
.0. get the WPF Toolkit
.1. run the MSI for the WPF Toolkit
.2. Within Visual Studio, Drag/drop from the toolbox - specifically the Data Visualization group - into the UI Designer. It looks like this in the VS toolbox:
If you don't want to use the designer, hand-craft the xaml. It looks like this:
<toolkit:AutoCompleteBox
ToolTip="Enter the path of an assembly."
x:Name="tbAssembly" Height="27" Width="102"
Populating="tbAssembly_Populating" />
...where the toolkit namespace is mapped this way:
xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
.3. Provide the code for the Populating
event. Here's what I used:
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();
}
}
It works, just the way you'd expect. It feels professional. There are none of the anomalies that the codeproject control exhibits. This is what it looks like:
Thanks to Matt for the pointer to the WPF toolkit.
The newest drop of the WPF Toolkit includes an AutoCompleteBox. It's a free set of controls from Microsoft, some of which will be included in .NET 4.
Jeff Wilcox - Introducing the AutoCompleteBox
这篇关于WPF:自动完成文本框,...再次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!