how to search and change items in combobox?

mc 4,111 Reputation points
2023-07-21T13:45:54.7633333+00:00

Can I search in the combobox ?

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
752 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 83,206 Reputation points
    2023-07-21T17:07:14.79+00:00

    You can use an ObservableCollection.

    For example :

      <ComboBox x:Name="cmb1" Header="Test ComboBox" Height="60" Width="296" Margin ="100,10,0,0"                          ItemsSource="{x:Bind itemList}"/>
    
      System.Collections.ObjectModel.ObservableCollection<string> itemList = new System.Collections.ObjectModel.ObservableCollection<string>();
    

    Fill ComboBox at beginning :

      for (int i = 0; i <= 10; i++)
          itemList.Add("Item " + i.ToString("D2"));
    

    Test updating an item :

                    string sItemToSearch = "Item 05";
                    string sNewItem = "New Item";
                    int nIndex = itemList.IndexOf(sItemToSearch);
                    if (nIndex != -1)
                    {
                        if ((string)cmb1.SelectedItem == sItemToSearch)
                        {
                            itemList[nIndex] = sNewItem;
                            cmb1.SelectedItem = itemList[nIndex];
                        }
                        else
                            itemList[nIndex] = sNewItem;
                    }
    

0 additional answers

Sort by: Most helpful