gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:26:15 27 4
gpt4 key购买 nike

This other SO question询问 WPF 中的自动完成文本框。有几个人已经构建了这些,其中给出的答案之一表明 this codeproject article .

但我还没有找到任何与 WinForms 自动完成文本框相比的 WPF 自动完成文本框。 codeproject 示例可以正常工作,...

alt text

...但是

  • 它的结构不是可重用的控件或 DLL。这是我需要嵌入到每个应用程序中的代码。
  • 它仅适用于目录。它没有用于设置自动完成源是否仅是文件系统目录、文件系统文件或......等的属性。当然,我可以编写代码来执行此操作,但是......我宁愿使用其他人已经编写的代码。
  • 它没有设置弹出窗口大小等的属性。
  • 有一个弹出列表框显示可能的补全。浏览该列表时,文本框不会更改。将焦点集中在列表框中时键入字符不会导致文本框更新。
  • 将焦点从列表框移开不会使弹出列表框消失。这很令人困惑。

所以,我的问题:

*是否有人有一个免费的 WPF 自动完成文本框可以工作,并提供优质的 UI 体验?*

<小时/>

回答

我是这样做的:

.0。获取WPF Toolkit

.1。运行 WPF 工具包的 MSI

.2。在 Visual Studio 中,从工具箱(特别是数据可视化组)拖/放到 UI 设计器中。在VS工具箱中看起来像这样:

alt text

如果您不想使用设计器,请手工制作 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"
<小时/>

.3。提供 Populate 事件的代码。这是我使用的:

<小时/>
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();
}
}
<小时/>

它有效,正如您所期望的那样。感觉很专业。 codeproject 控件没有表现出任何异常情况。这是它的样子:

alt text

<小时/>

Thanks to Matt for the pointer WPF 工具包。

最佳答案

WPF Toolkit的最新下降包括一个自动完成框。它是 Microsoft 提供的一组免费控件,其中一些将包含在 .NET 4 中。

Jeff Wilcox - Introducing the AutoCompleteBox

关于WPF:自动完成文本框,...再次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2338690/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com