2
votes

I am new to WPF, trying to fix up an autosuggest combobox on keyup event

Xaml Code : here is my xaml for this:

<ComboBox x:Name="party_list" Margin="10,15,0,6" Grid.Column="1" AllowDrop="True" IsTextSearchEnabled="False" IsEditable="True" KeyUp="party_list_KeyUp" >

C# Code : Code behind


    public partial class page_addsale : Page
    {

        List<string> nameList { get; set; }
        DataTable data = new DataTable();
        List<string> autoList = new List<string>();

        public page_addsale()
        {
            InitializeComponent();

            nameList = new List<string>();

            con.Open();
            OleDbDataAdapter ad = new OleDbDataAdapter("select id, party_name from party_list", con);
            ad.Fill(data);
            con.Close();

            party_list.ItemsSource= data.DefaultView;
            party_list.DisplayMemberPath = "party_name";
            party_list.SelectedValuePath = "id";

            string[] arr = data.AsEnumerable().Select<System.Data.DataRow, String>(x => x.Field<String>("party_name")).ToArray();
            nameList.AddRange(arr);            

        }       

        private void party_list_KeyUp(object sender, KeyEventArgs e)
        {
            party_list.ItemsSource = null;
            var names = from n in nameList where (n.StartsWith(party_list.Text)) select n;

            foreach (string name in names)
            {
                autoList.Add(name.ToString());
            }
            try
            {
                if (party_list.Text.Length > 0)
                {
                   if (autoList.Count > 0)
                   {
                        party_list.ItemsSource = autoList;
                        party_list.IsDropDownOpen = true;

                   }                  
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

where the items are updating when the itemssource binding is changed But does not show in combobox

1

1 Answers

1
votes

You need to clear before assigning new party_list.ItemsSource = autoList;

party_list.DisplayMemberPath = "";
party_list.SelectedValuePath = "";

But I won't suggest like this create Class. and convert your data table to its class. so that u will have constancy.