Add items to comboBox in WPF

前端 未结 7 1856
谎友^
谎友^ 2021-02-03 21:38

When I have added a comboBox to the WPF window, how do I add items to the comboBox? Int the XAML code for the design or in NameOfWindow.xaml.cs file?

7条回答
  •  [愿得一人]
    2021-02-03 22:04

    Use this

    string[] str = new string[] {"Foo", "Bar"};
    
    myComboBox.ItemsSource = str;
    myComboBox.SelectedIndex = 0;
    

    OR

    foreach (string s in str)
        myComboBox.Items.Add(s);
    
    myComboBox.SelectedIndex = 0;      
    

提交回复
热议问题