在WPF中关闭组合框中的autoComplete [英] Turn off autoComplete in Combobox in wpf

查看:50
本文介绍了在WPF中关闭组合框中的autoComplete的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.NET Framework 4.0来构建我的应用程序。

I am using .NET framework 4.0 to build my application.

我有一个组合框,其中我想关闭组合框的建议附加模式。相反,我想要仅建议模式。

I have a combobox in which I want to turn off suggest-append mode of combobox. Instead I want suggest-only mode.

在许多问题中,用户要求关闭autoComplete功能,而在我得到相同答案的任何地方。即将IsTextSearchEnabled设置为False。

In many questions users ask for turning autoComplete feature off and everywhere I got the same answer. i.e. set IsTextSearchEnabled to False.

当IsTextSearchEnabled = True

When IsTextSearchEnabled = True

当IsTextSearchEnabled = False

When IsTextSearchEnabled = False

我想要的是:

当用户在组合框上按Enter时,我希望将该项目附加到组合框的文本框中。

When User Presses Enter on the Combobox I want the Item to be appended to the textbox of the combobox.

在WPF中是否有可能?

Is this thing possible in WPF?

推荐答案

就像在这里承诺的演示一样。如您所见,我做了我在评论中解释的内容。我听了文本更改事件。

Like promised here is the demo. As you can see I did what I explained in my comments. I listened to text changed event.

查看一下:

<Grid>
    <local:MyComboBox x:Name="comboBox" IsEditable="True"
              VerticalAlignment="Center"
              IsTextSearchEnabled="True">
        <ComboBoxItem>hello</ComboBoxItem>
        <ComboBoxItem>world</ComboBoxItem>
        <ComboBoxItem>123</ComboBoxItem>
    </local:MyComboBox>
</Grid>

public class MyComboBox : ComboBox
{
    private string myValue;
    private bool needsUpdate;

    public override void OnApplyTemplate()
    {
        TextBox tbx = this.GetTemplateChild("PART_EditableTextBox") as TextBox;

        tbx.PreviewKeyDown += (o, e) =>
        {
            this.needsUpdate = true;
        };

        tbx.TextChanged += (o, e) =>
            {
                if (needsUpdate)
                {
                    myValue = tbx.Text;
                    this.needsUpdate = false;
                }
                else
                {
                    tbx.Text = myValue;
                }
            };

        base.OnApplyTemplate();
    }
}

这篇关于在WPF中关闭组合框中的autoComplete的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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