带有 IsEditable="True" 的 WPF 组合框- 我怎样才能表明没有找到匹配? [英] WPF ComboBox with IsEditable="True" - How can I indicate that no match was found?

查看:20
本文介绍了带有 IsEditable="True" 的 WPF 组合框- 我怎样才能表明没有找到匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下面的简单文本框为例:

Using the following simple text box as an example:

<ComboBox IsEditable="True" SelectedItem="{Binding}">
    <ComboBoxItem>Angus/ComboBoxItem>
    <ComboBoxItem>Jane</ComboBoxItem>
    <ComboBoxItem>Steve</ComboBoxItem>
</ComboBox>

我想让用户通过输入名称来找到他们的选择,所以我将 IsEditable 设置为 true.绑定到 SelectedItem 的属性的可接受值是列表中的任何一个选项,或者没有选择 (null).问题是,如果有人输入了不在列表中的名字,默认情况下没有错误指示.

I would like to allow the user to find their selection by typing in a name, so I have set IsEditable equal to true. Acceptable values for the property bound to SelectedItem are any one of the options in the list, or no selection (null). The problem is, there is no error indication by default in the event someone types in a name that isn't in the list.

例如:用户可以键入Bob",导致 SelectedItem 属性为 null,但没有意识到列表中不存在 Bob.相反,我想在 ComboBox 的 Text 属性不是 null 或空且 SelectedItemnull<时提供视觉指示/em>,然后阻止他们再打字?

For example: a user could type "Bob", causing the SelectedItem property to be null, but not realize that Bob doesn't exist in the list. Instead I would like to provide a visual indication as soon as the ComboBox's Text property is not null or empty AND SelectedItem is null, and stop them from typing any more?

我最初的想法是自定义验证规则,但我不知道如何访问组合框的 Text 和 SelectedItem 属性.

My initial thought was a custom validation rule, but I don't know how to access both the Text and SelectedItem properties of the combobox.

推荐答案

作为初学者,您可能希望让用户查看他们是否正在输入可用选项之一.

As a starter, you might want to let the user see if they are typing in one of the available options.

1) 在线搜索自动完成组合框".

1) Search "autocomplete combobox" online.

2) 看看这些:

http://weblogs.asp.net/okloeten/存档/2007/11/12/5088649.aspx

http://www.codeproject.com/KB/WPF/WPFCustomComboBox.aspx

3) 也试试这个:

    <ComboBox IsEditable="true" TextSearch.TextPath="Content">
        <ComboBoxItem Content="Hello"/>
        <ComboBoxItem Content="World"/>
    </ComboBox>

上面的代码片段是提供您正在寻找的视觉指示"的原始方式.如果用户输入h",则hello"将出现在输入文本框中.但是,这本身并没有阻止用户输入非法字符的机制.

The above code snippet is a primite way to provide that "visual indication" you're looking for. If the user types in 'h', then 'hello' will appear in the input textbox. However, this on its own won't have a mechanism to stop the user from typing in an illegal character.

4) 这是一个更高级的版本:

4) This is a more advanced version:

    <ComboBox Name="myComboBox" IsEditable="true" KeyUp="myComboBox_KeyUp">
        <ComboBoxItem Content="Hello"/>
        <ComboBoxItem Content="World"/>
        <ComboBoxItem Content="WPF"/>
        <ComboBoxItem Content="ComboBox"/>
    </ComboBox>

代码隐藏:

    private void myComboBox_KeyUp(object sender, KeyEventArgs e)
    {
        // Get the textbox part of the combobox
        TextBox textBox = myComboBox.Template.FindName("PART_EditableTextBox", myComboBox) as TextBox;

        // holds the list of combobox items as strings
        List<String> items = new List<String>();

        // indicates whether the new character added should be removed
        bool shouldRemove = true;

        for (int i = 0; i < myComboBox.Items.Count; i++)
        {
            items.Add(((ComboBoxItem)myComboBox.Items.GetItemAt(i)).Content.ToString());
        }

        for (int i = 0; i < items.Count; i++)
        {
            // legal character input
            if(textBox.Text != "" && items.ElementAt(i).StartsWith(textBox.Text))
            {
                shouldRemove = false;
                break;
            }
        }

        // illegal character input
        if (textBox.Text != "" && shouldRemove)
        {
            textBox.Text = textBox.Text.Remove(textBox.Text.Length - 1);
            textBox.CaretIndex = textBox.Text.Length;
        }
    }

在这里,一旦我们检测到没有组合框项目以文本框中的文本开头,我们就不会让用户继续输入.我们删除添加的字符并等待另一个字符.

Here, we don't let the user continue typing in once we detect that no combobox item starts with the text in the textbox. We remove the character added and wait for another character.

这篇关于带有 IsEditable="True" 的 WPF 组合框- 我怎样才能表明没有找到匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆