【问题标题】:WPF: AutoComplete TextBox, ...againWPF:自动完成文本框,...再次
【发布时间】:2011-01-21 06:35:27
【问题描述】:

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

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

...但是

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

所以,我的问题:

*有没有人有免费的 WPF 自动完成文本框可以工作,并提供高质量的 UI 体验?*


回答

我是这样做的:

.0。获取WPF Toolkit

.1。为 WPF 工具包运行 MSI

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

如果您不想使用设计器,请手工制作 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。提供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();
    }
}

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


Thanks to Matt for the pointer 到 WPF 工具包。

【问题讨论】:

    标签: wpf textbox autocomplete


    【解决方案1】:

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

    Jeff Wilcox - Introducing the AutoCompleteBox

    【讨论】:

    【解决方案2】:

    我是这样做的:

    .1。为 WPF 工具包运行 MSI

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

    或者,手工制作 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。提供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();
        }
    }
    

    感谢 Matt 提供指向 WPF 工具包的指针。

    【讨论】:

      【解决方案3】:

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

      我发现它使用提供者模式进行搜索非常直观。

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

      【讨论】:

      • intellibox.codeplex.com 似乎在 2013 年 10 月 1 日才更新并包含单个控件。 (谢谢,瑞克)
      • 是的,我已经有一段时间没有做这个了,很高兴知道从那时起它已经引起了一些关注。谢谢!
      【解决方案4】:

      Mindscape 还提供了一个 3 free controls,包括一个 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>
      

      【讨论】:

        【解决方案5】:

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

        【讨论】:

        • 我知道这是您的项目,看起来不错,但您应该做的不仅仅是在此处添加链接。添加一些示例代码、说明,说明为什么它是 OP 的可能解决方案。 StackOverflow 本质上是一个问答 wiki,因此用户希望在答案中看到足够的细节,以了解该解决方案是否适用于他们,而不会被从该站点带走。通过各种方式将人们引导到 codeplex 站点以获取更多信息。
        • 我花了一上午的时间检查我在网上不同地方找到的自动完成文本框。这是迄今为止最好的,也是我将在我的项目中使用的。
        猜你喜欢
        • 2010-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-24
        • 2011-04-18
        • 2010-09-22
        相关资源
        最近更新 更多