简体   繁体   English

如何限制组合框中的用户输入,以便您只能输入集合中的单词?

[英]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.

相关问题 如何限制用户在文本框中键入的字符数? - How do i limit the number of characters a user can type in a textbox? 我怎样才能让我的用户输入只接受特定的符号? - How can I make it so that my user input only accepts specific symbols? 如何冻结集合以便可以迭代它? - How do I freeze a collection so that I can iterate through it? 如何存储列表/数组/任何类型的集合。 或键入名称,以便可以将它们作为类型传递给函数 - How do I store a List/Array/ any collection of types. Or type names so they can be passed as a type to a function 如何打开对象类型接口的toString,以便获得加载用户控件的路径? - How do I turn the toString of an object type interface so I can get the path to load the user control? 如何在ConfigurationSection类型的集合中进行搜索? - How do I search within a collection of type ConfigurationSection? 我怎样才能概括这两个协程,所以我只使用一个? (在参数中输入一个布尔值并从内部设置) - How can I generalize these two coroutines so I only use one? (Input a boolean into the parameters and set from within) 我该怎么办才能选择一个复选框? - How can I do so only one checkbox can be selected? 如何等待为comboBox创建句柄,以便可以更改selectedIndex? - How do I wait for a handle to be created for a comboBox, so that I can change the selectedIndex? 如何限制图表上的数据收集? - How do I limit the data collection on a chart?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM