[英]How do I limit user input in a combobox, So that u can only type words that are within the collection?
To clarify I have a combobox with an observable collection bound to its itemsource property. 为了澄清我有一个组合框,其可观察的集合绑定到其itemsource属性。 I want user to be able to type in only items that ar present in the collection.
我希望用户只能输入集合中存在的项目。 whilst keeping the
同时保持
IsTextSearchEnabled="true"
Functionality. 功能。 So I've been looking online for an example on how to do this.
所以我一直在网上寻找一个如何做到这一点的例子。 Till now I figured i should use the
直到现在我想我应该使用
<ComboBox TextInput="cbb1_TextInput" />
Event and then searching the collection. 事件然后搜索集合。 But me feeble attempt's cant maintain the auto-complete feature, selectedindex gets jumbled and other unwanted behavior.
但我虚弱的尝试不能维持自动完成功能,selectedindex混乱和其他不需要的行为。 Since I'm quite a noob in wpf I wonder if there are solutions that only use the XAML part?
因为我是wpf中的一个菜鸟,我想知道是否有只使用XAML部分的解决方案?
EDIT: I tried something like this: 编辑:我尝试过这样的事情:
private void fase1cbb1_KeyUp(object sender, KeyEventArgs e)
{
ComboBox cb = (ComboBox)sender;
bool match = false;
TextBox tb = (TextBox)cb.Template.FindName("PART_EditableTextBox", cb);
if (tb.Text.Length > 0)
{
foreach (MenuItem MI in cb.Items)
{
if (MI.Text.StartsWith(tb.Text))
{
match = true; ;
}
}
if (!match)
{
int len = tb.Text.Length;
if (len > 0)
{
tb.Text = tb.Text.Substring(0, len - 1);
tb.SelectionStart = len;
}
}
}
}
But as soon as there is no match anymore there is no more selected item, and no more autocomplete/textsearch. 但是一旦没有匹配,就没有更多的选定项目,也没有更多的自动完成/文本搜索。
thnx for any advice or examples. thnx任何建议或例子。
SOLUTION: WPF ComboBox with IsEditable="True" - How can I indicate that no match was found? 解决方案: 带有IsEditable =“True”的WPF ComboBox - 如何指示未找到匹配项?
If you scroll all the way down to the bottom of the ComboBox documentation on MSDN you'll find that there's one very simple possible answer: set your ComboBox.IsEditable
to false. 如果你一直向下滚动到MSDN上的ComboBox文档的底部,你会发现有一个非常简单的答案:将你的
ComboBox.IsEditable
设置为false。 The user can still select items in the editor by typing the prefix into the box, but they'll only be able to enter values that already appear in the list. 用户仍然可以通过在框中键入前缀来选择编辑器中的项目,但是他们只能输入已经出现在列表中的值。
The downside is that you don't get any of the normal "TextBox"-like behavior, in particular, you can't copy/paste the selected item out of the combo box. 缺点是您没有获得任何正常的“TextBox”类似行为,特别是您无法将所选项目复制/粘贴到组合框之外。 If that's also an issue for you, let us know, but that is the typical behavior for DropDownList style ComboBoxes in Windows anyway.
如果这也是一个问题,请告诉我们,但这仍然是Windows中DropDownList样式ComboBox的典型行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.