WPF AutoCompleteBox - 如何限制它仅从建议列表中选择?
我想限制 WPF AutoCompleteBox(wpf 工具包控件)仅从建议列表中选择一个项目。它不应该允许用户输入他们想要的任何内容。
有人可以建议我如何实现这个吗?任何示例代码都值得赞赏。
I would like to restrict WPF AutoCompleteBox (wpf toolkit control) to select an item only from the suggestion list. It should not allow users to type whatever they want.
Can someone suggest me how to implement this? Any sample code is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我是这样做的。创建派生类并重写 OnPreviewTextInput。将您的集合设置为控件的 ItemsSource 属性,它应该可以正常工作。
Here's how I did it. Create a derived class and override OnPreviewTextInput. Set your collection to the control's ItemsSource property and it should work nicely.
您可以通过“Priview key down”事件来限制用户。我希望它能起作用...
You can restrict user by Priview key down event. I hope it will work...
如果将其数据绑定到属性,例如,
如果输入的某些文本与 ItemsSource 中的任何内容都不匹配,则 SelectedItem 将等于 null。
在属性的 set 方法中,您可以不设置该值,因为它为 null,并且该属性将保留其原始值。
If you are databinding it to a property, like this for an example
If some text is entered which doesn't match anything in the ItemsSource, the SelectedItem will be equal to null.
In the set method of your property, you can just not set the value because it's null, and the Property will keep it's original value.