Khushi
Khushi

Reputation: 1051

Turn off autoComplete in Combobox in wpf

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.

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

When IsTextSearchEnabled = True

enter image description here

When IsTextSearchEnabled = False

enter image description here

What I want is :

enter image description here

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

Is this thing possible in WPF?

Upvotes: 3

Views: 4793

Answers (1)

dev hedgehog
dev hedgehog

Reputation: 8801

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

Check it out:

<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();
    }
}

Upvotes: 4

Related Questions