Click here to Skip to main content
14,698,814 members
Please Sign up or sign in to vote.
4.20/5 (5 votes)
See more:
Guy's after trying to autocomplete a textbox i move towards now to an autocomplete combobox.Because i cannot add all controls to a textbox.But in combobox,i did that.But a problem also stands there.When i type in combobox it's popup filters the items according to typed text nicely,but i need to select a option by mouse,cannot by keyboard arrow's.I tried to solve it with keydown event but somehow it cannot focus combobox i think.Someone can suggest me here what wrong i am doing or how can i enable keyboard arrowkey events?Below are my codes:

XML:
<ComboBox Height="23" HorizontalAlignment="Left" Margin="160,236,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" IsEditable="True" ItemsSource="{Binding}" KeyDown="comboBox1_KeyDown">
    <ComboBox.Resources>
        <sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}">0</sys:Double>
    </ComboBox.Resources>
</ComboBox>


C#:
    public partial class MainWindow : Window
    {
        List<string> nameList;
        
        public MainWindow()
        {
            InitializeComponent();
            nameList = new List<string> 
            {
                "A0-Word","B0-Word","C0-Word",
                "A1-Word","A111","A11122","B1-Word","C1-Word",
                "B2-Word","C2-Word",
                "C3-Word",
            };

           comboBox1.DataContext = nameList;

            comboBox1.Loaded += delegate
            {
                System.Windows.Controls.TextBox textBox = comboBox1.Template.FindName("PART_EditableTextBox", comboBox1) as System.Windows.Controls.TextBox;
                Popup popup = comboBox1.Template.FindName("PART_Popup", comboBox1) as Popup;
                if (textBox != null)
                {
                    textBox.TextChanged += delegate
                    {
                            comboBox1.Items.Filter += (item) =>
                            {
                                if (item.ToString().StartsWith(textBox.Text))
                                {
                                    popup.IsOpen = true;
                                    return true;
                                    
                                }
                                else
                                {
                                   // popup.IsOpen = false;
                                    return false;
                                }
                            };

                    };
                }
            };

            comboBox1.KeyDown+=new System.Windows.Input.KeyEventHandler(comboBox1_KeyDown);
            
        }

private void comboBox1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{

    if (e.Key == Key.Down)
    {
        e.Handled = true;
        comboBox1.Items.MoveCurrentToNext();
    }
}
Posted
Comments
Clifford Nelson 28-Jun-12 12:52pm
   
We have been looking at AutoComplete TextBox here, and had all sorts of problems. For whatever reason, it is difficult to get the combobox to completely work right for this feature. There are some really strange interactions. We also decided to go with the WPF toolbox version.
[no name] 24-Sep-13 6:39am
   
Ridoy did u get the solution for this..??i need the same help..
ridoy 24-Sep-13 9:09am
   
No,later i used a 3rd party control there.You can have a look at https://www.google.com/#q=Autocomplete+Combobox+in+WPF+c%23 and find which one will be helpful for you.

1 solution

FYI, there is nice auto-complete text box in WPF Toolkit (). Why don`t you use it?
   
Comments
Mortada.Issa 30-Jul-14 10:07am
   
The one in WPF Toolkit is a crap.. it does not work when you have two items in the list with the same text value.
Javad Mousavi 23-Jul-18 7:29am
   
How we can access this toolkit?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900