Che*_*eso 45 wpf textbox autocomplete
另一个SO问题询问WPF中的自动完成文本框.有几个人建了这些,其中一个答案就是这个代码项目的文章.
但我没有找到任何WPF自动完成文本框与WinForms自动完成文本框进行比较.codeproject示例有效,有点......
alt text http://i50.tinypic.com/sx2ej5.jpg
...但
所以,我的问题:
*没有任何人有一个免费的WPF自动完成文本框的作品,并提供优质的UI体验?*
回答
我是这样做的:
0.0.获得WPF工具包
0.1.运行WPF Toolkit的MSI
0.2.在Visual Studio中,从工具箱(特别是数据可视化组)拖放到UI设计器中.在VS工具箱中看起来像这样:
替代文字http://i49.tinypic.com/s12q6x.jpg
如果您不想使用设计师,请手工制作xaml.它看起来像这样:
<toolkit:AutoCompleteBox
ToolTip="Enter the path of an assembly."
x:Name="tbAssembly" Height="27" Width="102"
Populating="tbAssembly_Populating" />
Run Code Online (Sandbox Code Playgroud)
...工具箱命名空间以这种方式映射的位置:
xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
Run Code Online (Sandbox Code Playgroud)
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();
}
}
Run Code Online (Sandbox Code Playgroud)
它的工作原理,就像你期望的那样.感觉很专业.代码项目控件没有出现任何异常.这就是它的样子:
alt text http://i50.tinypic.com/24qsopy.jpg
Mat*_*ton 32
WPF Toolkit的最新版本包括一个AutoCompleteBox.它是微软的一套免费控件,其中一些将包含在.NET 4中.
Jeff Wilcox - 介绍AutoCompleteBox
Che*_*eso 17
我是这样做的:
0.1.运行WPF Toolkit的MSI
0.2.在Visual Studio中,从工具箱(特别是数据可视化组)拖放到UI设计器中.在VS工具箱中看起来像这样:
替代文字http://i49.tinypic.com/s12q6x.jpg
或者,手工制作xaml.它看起来像这样:
<toolkit:AutoCompleteBox
ToolTip="Enter the path of an assembly."
x:Name="tbAssembly" Height="27" Width="102"
Populating="tbAssembly_Populating" />
Run Code Online (Sandbox Code Playgroud)
...工具箱命名空间以这种方式映射的位置:
xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
Run Code Online (Sandbox Code Playgroud)
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();
}
}
Run Code Online (Sandbox Code Playgroud)
感谢Matt指向WPF工具包的指针.