locked
Implimenting AutoComplete combobox RRS feed

  • Question

  •  

    Hi All

     

    I need to Impliment Search functionality with my  "System.Windows.Controls.ComboBox ",  Previously I was using System.Windows.Forms.ComboBox.  For that I had implinented this functionality with

     

     

    ComboBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;

    ComboBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;

     

    But it is not woking with my current component. In that I set the property IsTextSearchEnabled but while entering the search string it is showing all the elements in the combobox. But I need to display only the list which match with my search string.

     

    It will be very helpful for me if you can suggest me the soulution for inplimenting the AutoComplete functionality with WPF.

     

    thanks,
    Hareesh Bharadwaj

    Sunday, May 25, 2008 12:29 PM

Answers

  • Hello, It seems that WPF ComboBox doesn't support auto complete directly. Here is one sample.

    Code Snippet

    <Window x:Class="MiscellaneousAnswers.AutoCompleteComboBoxDemo"}

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="AutoCompleteComboBoxDemo" Height="300" Width="300">

      <Grid>

        <ComboBox Height="30" Width="200" ItemsSource="{Binding}" IsEditable="True" Name="comboBox">

          <ComboBox.ItemsPanel>

            <ItemsPanelTemplate>

              <VirtualizingStackPanel IsVirtualizing="True" IsItemsHost="True"/>

            </ItemsPanelTemplate>

          </ComboBox.ItemsPanel>

        </ComboBox>

      </Grid>

    </Window>

     

    public partial class AutoCompleteComboBoxDemo : Window

    {

     

        public AutoCompleteComboBoxDemo()

        {

            InitializeComponent();

            this.DataContext = GetData();

            comboBox.Loaded += delegate

            {

                TextBox textBox = comboBox.Template.FindName("PART_EditableTextBox", comboBox) as TextBox;

                Popup popup = comboBox.Template.FindName("PART_Popup", comboBox) as Popup;

                if (textBox != null)

                {

                    textBox.TextChanged += delegate

                    {

                        popup.IsOpen = true;

                        comboBox.Items.Filter += a =>

                        {

                            if (a.ToString().StartsWith(textBox.Text))

                            {

                                return true;

                            }

                            return false;

                        };

                    };

                }

            };

        }

     

        public List<String> GetData()

        {

            List<String> data = new List<String>();

            for (Int32 i = 0; i < 100; i++)

            {

                data.Add(i.ToString() + "Item");

                data.Add(i.ToString() + "Item");

                data.Add(i.ToString() + "Item");

                data.Add(i.ToString() + "Item");

                data.Add(i.ToString() + "Item");

            }

            return data;

        }


    • Marked as answer by Marco Zhou Monday, June 2, 2008 2:35 AM
    Sunday, May 25, 2008 1:11 PM
  • I found that ComboBox.IsTextSearchEnabled wasn't quite what I needed, so I used the Items.Filter property. I just found an example of how to use it here:

    http://blogs.vertigo.com/personal/alanl/Blog/Lists/Posts/Post.aspx?ID=32
    • Marked as answer by Marco Zhou Monday, June 2, 2008 2:34 AM
    Tuesday, May 27, 2008 12:05 AM

All replies

  • Hello, It seems that WPF ComboBox doesn't support auto complete directly. Here is one sample.

    Code Snippet

    <Window x:Class="MiscellaneousAnswers.AutoCompleteComboBoxDemo"}

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="AutoCompleteComboBoxDemo" Height="300" Width="300">

      <Grid>

        <ComboBox Height="30" Width="200" ItemsSource="{Binding}" IsEditable="True" Name="comboBox">

          <ComboBox.ItemsPanel>

            <ItemsPanelTemplate>

              <VirtualizingStackPanel IsVirtualizing="True" IsItemsHost="True"/>

            </ItemsPanelTemplate>

          </ComboBox.ItemsPanel>

        </ComboBox>

      </Grid>

    </Window>

     

    public partial class AutoCompleteComboBoxDemo : Window

    {

     

        public AutoCompleteComboBoxDemo()

        {

            InitializeComponent();

            this.DataContext = GetData();

            comboBox.Loaded += delegate

            {

                TextBox textBox = comboBox.Template.FindName("PART_EditableTextBox", comboBox) as TextBox;

                Popup popup = comboBox.Template.FindName("PART_Popup", comboBox) as Popup;

                if (textBox != null)

                {

                    textBox.TextChanged += delegate

                    {

                        popup.IsOpen = true;

                        comboBox.Items.Filter += a =>

                        {

                            if (a.ToString().StartsWith(textBox.Text))

                            {

                                return true;

                            }

                            return false;

                        };

                    };

                }

            };

        }

     

        public List<String> GetData()

        {

            List<String> data = new List<String>();

            for (Int32 i = 0; i < 100; i++)

            {

                data.Add(i.ToString() + "Item");

                data.Add(i.ToString() + "Item");

                data.Add(i.ToString() + "Item");

                data.Add(i.ToString() + "Item");

                data.Add(i.ToString() + "Item");

            }

            return data;

        }


    • Marked as answer by Marco Zhou Monday, June 2, 2008 2:35 AM
    Sunday, May 25, 2008 1:11 PM
  •  

    Hi Yiling Lai

     

    Thanks for your kind help. But in my case it is not working fine. When i tried to impliment the

    comboBox.Items.Filter += a =>

    {

                            if (a.ToString().StartsWith(textBox.Text))

                            {

                                return true;

                            }

                            return false;

                        };

    It is showing an error that =>  is not supported.

     

    Thanks

    Hareesh

    Monday, May 26, 2008 9:48 AM
  •  

    Hi Yiling Lai

      

    I just need  the MouseLeftButtonDown event when i select an item from the combobox list.

     

    MouseLeftButtonDown  event is working fine when i click on the combobox textblock.

     

    I tried to extract the list as a popup by

    popup = comboBox.Template.FindName("PART_Popup", comboBox) as Popup;

     

    But the MouseLeftButtonDown  event is not working with popup also. But popup.

    .MouseRightButtonUp event is working.

     

     

    It will be very helpful for me if you can suggest me the soulution for this functionality with WPF.

     

    thanks,
     Hareesh Bharadwaj

    Monday, May 26, 2008 4:07 PM
  • Hello Hareesh,

     Yiling is using a lambda expression (=>) which is only supported in C# 3.0 (.NET framework 3.5).

    Luc
    Monday, May 26, 2008 5:16 PM
  • Hi Yiling,

     

     Would you happen to know of a way to disable the combobox "autocomplete first matching item" feature when IsEditable is True? The autoselection is causing quite a bit of pain when backspacing.

     

    Thanks,

    Luc

     

    Monday, May 26, 2008 5:22 PM
  • Nevermind, ComboBox.IsTextSearchEnabled does the trick (more or less Smile

    Monday, May 26, 2008 6:01 PM
  • I found that ComboBox.IsTextSearchEnabled wasn't quite what I needed, so I used the Items.Filter property. I just found an example of how to use it here:

    http://blogs.vertigo.com/personal/alanl/Blog/Lists/Posts/Post.aspx?ID=32
    • Marked as answer by Marco Zhou Monday, June 2, 2008 2:34 AM
    Tuesday, May 27, 2008 12:05 AM
  • You can try to use Windows Forms ComboBox control in WPF. The Windows Forms control can be used in WPF application.

     

    Best Regards,

    Wei Zhou

    Tuesday, May 27, 2008 6:03 AM
  • Hi Yiling Lai

    The solution provided by you  is working fine for me except i am not able to select the combobox items with key up/down.

    Please help

    Thanks

    Sharath

    Monday, June 8, 2009 8:13 AM
  • The above URL not found.. !!!!
    Tuesday, March 11, 2014 6:03 AM
  • when i typed '1' it lists all "1Item"  but not 10Item,11Item....
    Please help on this
    Tuesday, March 11, 2014 6:05 AM