【WPF】自动完成/智能提示的文本框(AutoCompleteBox)
使用了插件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)的更多相关文章
- Android实现智能提示的文本输入框AutoCompleteTextView
今天我们要讲一个十分简单的内容,就是一个安卓控件的使用,用法很简单,但是很常用的一个.这里我用两种不同的写法来处理.当然,无论用哪一种写法,效果都是一样的. 我们先来看效果图. 要实现这种效果十分简单 ...
- WPF编程:textbox控件文本框数据显示最后一行
WPF编程:textbox控件文本框数据显示最后一行 TextBox控件在接收大量数据的时候,滚动条一般在最上方,如何使滚动条随着数据的接收而向下滚动呢?比如有一个TextBox'控件txbRecvD ...
- JS案例 - 可自动伸缩高度的textarea文本框
文本框的默认现象: textarea如果设置cols和rows来规定textarea的尺寸,那么textarea的默认宽高是这俩属性设置的值,可以通过鼠标拖拽缩放文本框的尺寸. textarea如果设 ...
- WPF TextBox 获得焦点后,文本框中的文字全选中
textbox.GotFocus 事件处理 Textbox.SelectAll() 是不行的, 这样处理会发生的情况是: 1) textbox1 当前没有焦点, 内容为 someText. 2) 鼠标 ...
- Jquery实现文本框输入提示
一些用户体验好的表单都会在文本框里设置输入提示,文本框获取焦点时,提示内容消息,如果未输入,失去焦点时又会出现提示. 网上找到一个比较好用的控件jquery.inputDefault.js 使用方法: ...
- Chrome表单文本框自动填充黄色背景色样式
chrome表单自动填充后,input文本框的背景会变成偏黄色的,这是由于chrome会默认给自动填充的input表单加上input:-webkit-autofill私有属性,然后对其赋予以下样式: ...
- chrome表单自动填充导致input文本框背景变成偏黄色问题解决
chrome表单自动填充后,input文本框的背景会变成偏黄色的,想必大家都会碰到这种情况吧, 这是由于chrome会默认给自动填充的input表单加上input:-webkit-autofill私有 ...
- JS基础 浏览器弹出的三种提示框(提示信息框、确认框、输入文本框)
浏览器的三种提示框 alert() //提示信息框 confirm() //提示确认框 prompt() //提示输入文本框 1.alert( ) 提示信息框 <script> alert ...
- JQ三种提示框:提示信息框、确认框、输入文本框
浏览器的三种提示框: alert()提示信息框 confirm()提示确认框 prompt()提示输入文本框 1.alert()提示信息框 效果: 实现代码: <script> alert ...
随机推荐
- CentOS下Storm 1.0.0集群安装具体解释
本文环境例如以下: 操作系统:CentOS 6 32位 ZooKeeper版本号:3.4.8 Storm版本号:1.0.0 JDK版本号:1.8.0_77 32位 python版本号:2.6.6 集群 ...
- 微信小程序+PHP:动态显示项目倒计时(格式:4天7小时58分钟39秒)
1.一般我们说的显示秒杀都是指的单条数据,循环我没做. 效果: 2.wxml代码: <p class="endtime_act">距报名截止还有: <block ...
- 用UNetbootin轻松把Linux操作系统装进U盘
转自http://www.hackbase.com/tech/2009-02-14/51197.html1.下载Linux发行版的iso镜像. 如Hiweed GNU/Linux:http:// ...
- 用setitimer实现多个定时器
从这篇文章中可以看出,setitimer只能实现一个定时器,如果多次调用setitimer,旧值都会被覆盖掉. 如何用setitimer实现多个定时器呢?下面是我的一个实现,具体的方法是: 用链表从小 ...
- leetcode48:矩阵旋转
题目链接 输入一个N×N的方阵,要求不开辟新空间,实现矩阵旋转. 将点(x,y)绕原点顺时针旋转90度,变为(y,-x).原来的(-y,x)变为(x,y) class Solution(object) ...
- CMA概述
前言 本文是近期学习CMA模块的一个学习笔记,方便日后遗忘的时候,回来查询以便迅速恢复上下文. 学习的基本方法是这样的:一开始,我自己先提出了若干的问题,然后带着这些问题查看网上的资料,代码,最后整理 ...
- python标准库介绍——23 UserString 模块详解
==UserString 模块== (2.0 新增) ``UserString`` 模块包含两个类, //UserString// 和 //MutableString// . 前者是对标准字符串类型的 ...
- Chipscope使用
作者:桂. 时间:2017-08-07 06:47:31 链接:http://www.cnblogs.com/xingshansi/p/7297482.html 前言 Chipscope在FPGA调 ...
- Linux中的wget命令
Linux系统中的wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,我们经常要下载一些软件或从远程服务器恢复备份到本地服务器.wget支持HTTP,HTTPS和FTP协 ...
- Spring自动装配Bean详解
1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiring ‘byType 4. Auto-Wirin ...