使用了插件WPFToolKit。(直接在Nuget中搜即可)

使用方法参考这篇文章:

http://www.broculos.net/2014/04/wpf-autocompletebox-autocomplete-text.html

但是光参考上面的文章做还是有些小问题的,下面是我用WAF框架(MVVM)的一个小例子:

ShellWindow.xaml

<Window x:Class="WafApplication1.Presentation.Views.ShellWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:WafApplication1.Applications.ViewModels"
xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
mc:Ignorable="d" Title="{Binding Title}" Icon="{StaticResource ApplicationIcon}" Width="525" Height="350"
d:DataContext="{d:DesignInstance vm:ShellViewModel}"> <DockPanel>
<Grid>
<toolkit:AutoCompleteBox
Width="100" Height="30"
ItemsSource="{Binding BuildList}"
ValueMemberBinding="{Binding Name}"
FilterMode="Contains"
PreviewKeyDown="autoCompleteBox_PreviewKeyDown"><!-- 在AutoCompleteBox中注册PreviewKeyDown,键盘回车键选中列表Item -->
<toolkit:AutoCompleteBox.ItemTemplate>
<DataTemplate>
<Label
Content="{Binding Name}"
Width="100"
MouseLeftButtonUp="Label_MouseLeftButtonUp"/><!-- 在Item中注册MouseLeftButtonUp,鼠标左键点击选中列表Item -->
</DataTemplate>
</toolkit:AutoCompleteBox.ItemTemplate>
</toolkit:AutoCompleteBox>
</Grid>
</DockPanel>
</Window>

ShellWindow.xaml.cs 界面的后台代码。传递前台注册的鼠标左键事件和键盘Enter回车键事件。

private void Label_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Label label = sender as Label;
Build bulid = label.DataContext as Build;
houseTypeViewModel.Value.SelectBuildCommand.Execute(bulid);
} private void autoCompleteBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
AutoCompleteBox acBox = sender as AutoCompleteBox;
Build build = acBox.SelectedItem as Build;
houseTypeViewModel.Value.SelectBuildCommand.Execute(build);
}
}

ShellViewModel.cs

using System.ComponentModel.Composition;
using System.Waf.Applications;
using System.Windows.Input;
using WafApplication1.Applications.Views;
using System.Collections.ObjectModel; namespace WafApplication1.Applications.ViewModels
{
[Export]
internal class ShellViewModel : ViewModel<IShellView>
{
// 自动完成的文本框
private ObservableCollection<Build> buildList;
public ObservableCollection<Build> BuildList
{
get { return buildList; }
set { SetProperty(ref buildList, value); }
} // 自动完成文本框,用鼠标左键、键盘Enter键选中一个Item
private ICommand selectBuildCommand;
public ICommand SelectBuildCommand
{
get { return selectBuildCommand; }
set { SetProperty(ref selectBuildCommand, value); }
} [ImportingConstructor]
public ShellViewModel(IShellView view)
: base(view)
{
BuildList = new ObservableCollection<Build>();
} public void Show()
{
ViewCore.Show();
} private void Close()
{
ViewCore.Close();
} public class Build
{
public string Name { get; set; }
public int Id { get; set; }
} }
}

using System;
using System.ComponentModel.Composition;
using System.Waf.Applications;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WafApplication1.Applications.ViewModels;
using WafApplication1.Presentation.Views; namespace WafApplication1.Applications.Controllers
{
[Export]
internal class ApplicationController
{
private readonly ShellViewModel shellViewModel;
private ShellWindow shellView;
private readonly DelegateCommand selectBuildCommand; [ImportingConstructor]
public ApplicationController(ShellViewModel shellViewModel)
{
this.shellViewModel = shellViewModel;
shellView = shellViewModel.View as ShellWindow;
this.selectBuildCommand = new DelegateCommand(p => SelectBuildCommand((Builds)p));
} public void Initialize()
{
shellViewModel.SelectBuildCommand = selectBuildCommand; // 填充数据
shellViewModel.BuildList.Add(new ShellViewModel.Build() { Name = "一胞广场", Id = 0 });
shellViewModel.BuildList.Add(new ShellViewModel.Build() { Name = "二胞广场", Id = 1 });
shellViewModel.BuildList.Add(new ShellViewModel.Build() { Name = "三胞广场", Id = 2 });
shellViewModel.BuildList.Add(new ShellViewModel.Build() { Name = "四胞广场", Id = 3 });
shellViewModel.BuildList.Add(new ShellViewModel.Build() { Name = "五胞广场", Id = 4 });
shellViewModel.BuildList.Add(new ShellViewModel.Build() { Name = "六胞广场", Id = 5 });
shellViewModel.BuildList.Add(new ShellViewModel.Build() { Name = "七胞广场", Id = 6 });
shellViewModel.BuildList.Add(new ShellViewModel.Build() { Name = "八胞广场", Id = 7 }); } public void Run()
{
shellViewModel.Show();
} public void Shutdown()
{
} private void SelectBuildCommand(Builds bulid)
{
// do what you want...
}
}
}

测试效果:


一些小问题:

  • 需要指定FilterMode为Contains包含,否则只能从头开始匹配,如输入“桂”和“园”都不能匹配到“碧桂园”,只有输入“碧”和“碧桂”才行,这显然不符合用户需求。
  • 如果不使用ItemTemplate模板,则显示的会是对象的完整toString(名称空间 + 类名 + 属性名 + 属性值)
  • 使用ValueMemberBinding=”{Binding Name}”或是ValueMemberPaht=”Name”都行,貌似。
  • 关于选项的事件触发,我试过用给DataTemplate中的Label注册鼠标点击事件可以成功,但键盘Enter键键盘事件却无法触发!解决办法是给AutoCompleteBox控件添加PreviewKeyDown事件,而不是给Item添加事件!参考这 https://stackoverflow.com/questions/4996731/wpf-autocompletebox-and-the-enter-key
    • 在AutoCompleteBox中注册PreviewKeyDown,键盘回车键选中列表Item。
    • 在Item中注册MouseLeftButtonUp,鼠标左键点击选中列表Item。

【WPF】自动完成/智能提示的文本框(AutoCompleteBox)的更多相关文章

  1. Android实现智能提示的文本输入框AutoCompleteTextView

    今天我们要讲一个十分简单的内容,就是一个安卓控件的使用,用法很简单,但是很常用的一个.这里我用两种不同的写法来处理.当然,无论用哪一种写法,效果都是一样的. 我们先来看效果图. 要实现这种效果十分简单 ...

  2. WPF编程:textbox控件文本框数据显示最后一行

    WPF编程:textbox控件文本框数据显示最后一行 TextBox控件在接收大量数据的时候,滚动条一般在最上方,如何使滚动条随着数据的接收而向下滚动呢?比如有一个TextBox'控件txbRecvD ...

  3. JS案例 - 可自动伸缩高度的textarea文本框

    文本框的默认现象: textarea如果设置cols和rows来规定textarea的尺寸,那么textarea的默认宽高是这俩属性设置的值,可以通过鼠标拖拽缩放文本框的尺寸. textarea如果设 ...

  4. WPF TextBox 获得焦点后,文本框中的文字全选中

    textbox.GotFocus 事件处理 Textbox.SelectAll() 是不行的, 这样处理会发生的情况是: 1) textbox1 当前没有焦点, 内容为 someText. 2) 鼠标 ...

  5. Jquery实现文本框输入提示

    一些用户体验好的表单都会在文本框里设置输入提示,文本框获取焦点时,提示内容消息,如果未输入,失去焦点时又会出现提示. 网上找到一个比较好用的控件jquery.inputDefault.js 使用方法: ...

  6. Chrome表单文本框自动填充黄色背景色样式

    chrome表单自动填充后,input文本框的背景会变成偏黄色的,这是由于chrome会默认给自动填充的input表单加上input:-webkit-autofill私有属性,然后对其赋予以下样式: ...

  7. chrome表单自动填充导致input文本框背景变成偏黄色问题解决

    chrome表单自动填充后,input文本框的背景会变成偏黄色的,想必大家都会碰到这种情况吧, 这是由于chrome会默认给自动填充的input表单加上input:-webkit-autofill私有 ...

  8. JS基础 浏览器弹出的三种提示框(提示信息框、确认框、输入文本框)

    浏览器的三种提示框 alert() //提示信息框 confirm() //提示确认框 prompt() //提示输入文本框 1.alert( ) 提示信息框 <script> alert ...

  9. JQ三种提示框:提示信息框、确认框、输入文本框

    浏览器的三种提示框: alert()提示信息框 confirm()提示确认框 prompt()提示输入文本框 1.alert()提示信息框 效果: 实现代码: <script> alert ...

随机推荐

  1. 你的Android不好用,都是因为这几点原因

    Android早已是全球最大.用户最多的移动操作系统,不过它离全球最好用还差得很远. 大家随手就能举出些曾经历过的糟心体验,如手机卡顿!电量不禁用!广告弹窗老是出现!不过很少有人会追根寻底的去问为何如 ...

  2. 【ASP.NET Web API教程】2.4 创建Web API的帮助页面[转]

    注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容. 2.4 Creating a Help Page for a Web API2.4 创建W ...

  3. C#中遍历DataTable类型并删除行数据

    从数据库中读取出了DataSet类型的数据,通过dataSet.Tables[0]获得DataTable类型的数据. 这时候如果想批量修改dataTable中的内容,比如要删除dataTable中co ...

  4. .NET中那些所谓的新语法

    .NET中那些所谓的新语法之四:标准查询运算符与LINQ 摘要: 开篇:在上一篇中,我们了解了预定义委托与Lambda表达式等所谓的新语法,这一篇我们继续征程,看看标准查询运算符和LINQ.标准查询运 ...

  5. Accounting_会计基础

    会计基础 1.会计:是以货币为主要计量单位,反映和监督一个单位经济活动的一种经济管理工作. 2.会计核算职能:指以货币为主要计量单位,通过确认.记录.计算.报告等环节,对特定主体的经济活动进行记账.算 ...

  6. tomcat支持https的历程

    tomcat真是业界良心啊,文档写的详细无比. 一.https是什么? 简单的说,就是http+SSL/TLS 协议还是http,但是在传输层过程中使用了加密(涉及握手.秘钥分发.加密.解密等过程). ...

  7. C++拷贝函数的小结,关于变量的作用域等---ShinePans

    #include <iostream> using namespace std; class circle { private: double r0; public: circle(dou ...

  8. ie11 下 input 默认有 X 关闭按钮的问题

    &::-ms-clear, ::-ms-reveal{display: none;}

  9. appserv - 最简单的绑定路径

    补充:此方法比较low. 推荐伪静态:http://www.cnblogs.com/CyLee/p/5544119.html 找到路径C:\AppServ\Apache24\conf\httpd.co ...

  10. 利用ichart绘制网页图表

    首先,最好的教程在这里:ichartjs 有了这个网站,要绘制网页图表简直方便愉快! 接下来说一下使用方法~~~ 进入网站,点击在线设计器 在线设计器的使用方法就不说了,摸索一下就会了!关键在于两个地 ...