WPF ComboBox with IsEditable =" True" - 如何指示找不到匹配项? [英] WPF ComboBox with IsEditable="True" - How can I indicate that no match was found?

查看:667
本文介绍了WPF ComboBox with IsEditable =" True" - 如何指示找不到匹配项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下简单文本框作为示例:

 < ComboBox IsEditable =TrueSelectedItem ={Binding }> 
< ComboBoxItem> Angus / ComboBoxItem>
< ComboBoxItem> Jane< / ComboBoxItem>
< ComboBoxItem>史蒂夫< / ComboBoxItem>
< / ComboBox>

我想允许用户通过键入名称来找到他们的选择, IsEditable 等于true。与 SelectedItem 绑定的属性的可接受值是列表中的任何一个选项,或者没有选择( null )。问题是,在有人键入不在列表中的名称时,默认情况下没有错误指示。



例如:用户可以键入 Bob,导致 SelectedItem 属性为 null ,但不知道Bob不在列表中。相反,如果ComboBox的文本属性不是 null 或空,并且 SelectedItem / em>,并阻止他们再输入?



我最初的想法是一个自定义验证规则,但我不知道如何访问Text和SelectedItem

解决方案

作为开始,您可能希望让用户看到他们是否正在输入其中一个



1)在线搜索autocomplete combobox。

p>

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



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



3)还可以尝试:

 < ComboBox IsEditable =trueTextSearch.TextPath = > 
< ComboBoxItem Content =Hello/>
< ComboBoxItem Content =World/>
< / ComboBox>

上述代码片段是一种提供可视化指示的主要方式。如果用户键入h,那么hello将出现在输入文本框中。



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

 < ComboBox Name =myComboBoxIsEditable =trueKeyUp =myComboBox_KeyUp> 
< ComboBoxItem Content =Hello/>
< ComboBoxItem Content =World/>
< ComboBoxItem Content =WPF/>
< ComboBoxItem Content =ComboBox/>
< / ComboBox>

代码隐藏:

  private void myComboBox_KeyUp(object sender,KeyEventArgs e)
{
//获取组合框的textbox部分
TextBox textBox = myComboBox.Template.FindName(PART_EditableTextBox ,myComboBox)as TextBox;

//将组合框项目列表保存为字符串
List< String> items = new List< String>();

//指示是否应删除添加的新字符
bool shouldRemove = true;

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

for(int i = 0; i< items.Count; i ++)
{
//合法字符输入
if .text!=&&& items.ElementAt(i).StartsWith(textBox.Text))
{
shouldRemove = false;
break;
}
}

//非法字符输入
if(textBox.Text!=&& shouldRemove)
{
textBox.Text = textBox.Text.Remove(textBox.Text.Length - 1);
textBox.CaretIndex = textBox.Text.Length;
}
}

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


Using the following simple text box as an example:

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

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.

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?

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) Search "autocomplete combobox" online.

2) Check these out:

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

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

3) Also try this:

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

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) 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>

Code-behind:

    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.

这篇关于WPF ComboBox with IsEditable =&quot; True&quot; - 如何指示找不到匹配项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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