Adding predefined item to a ComboBox with ItemsSource

后端 未结 2 1754
死守一世寂寞
死守一世寂寞 2021-01-19 15:43

I\'m trying to add a predefined ComboBoxItem into my ComboBox which already has a ItemsSource property set. example:

(Select item)
Item 1
Item 2
Item 3
         


        
2条回答
  •  广开言路
    2021-01-19 16:26

    If you want to dynamically change the contents of items source, use ObservableCollection instead, so you will have access to Add() method.

    private ObservableCollection myStrings;
    
    public MyClass()
    {
        myStrings = new ObservableCollection();
        myControl.ItemsSource = myStrings;
    }
    
    private void AddNewItem(string item)
    {
        myStrings.Add(item);
    }
    

提交回复
热议问题