在WPF中将项添加到comboBox

当我将一个comboBox添加到WPF窗口时,如何将项添加到comboBox? 在设计的XAML代码中或在NameOfWindow.xaml.cs文件中?

场景1 – 您没有ComboBox项目的数据源
您可以使用静态值填充ComboBox,如下所示 –
来自XAML:

     

或者,从CodeBehind:

 private void Window_Loaded(object sender, RoutedEventArgs e) { comboBox1.Items.Add("X"); comboBox1.Items.Add("Y"); comboBox1.Items.Add("Z"); } 

场景2.a – 您有一个数据源,项目永远不会被更改
可以使用数据源填充ComboBox。 任何 IEnumerable类型都可以用作数据源。 您需要将它分配给ComboBox的ItemsSource属性,并且这样做会很好(这取决于您如何填充IEnumerable )。

场景2.b – 您有一个数据源,项目可能会更改
应该使用ObservableCollection作为数据源并将其分配给ComboBox的ItemsSource属性(由您来决定如何填充ObservableCollection )。 使用ObservableCollection可确保无论何时在数据源中添加或删除项目,更改都将立即反映在UI上。

最好是构建ObservableCollection并利用它

 public ObservableCollection list = new ObservableCollection(); list.Add("a"); list.Add("b"); list.Add("c"); this.cbx.ItemsSource = list; 

cbx是comobobox名称

另请阅读: List,ObservableCollection和INotifyPropertyChanged之间的区别

用这个

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

要么

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

您可以从XAML或.cs填写它。 用数据填充控件的方法很少。 您最好阅读有关WPF技术的更多信息,它可以根据您的需求以多种方式执行许多操作。 根据您的项目需求选择方法更为重要。 你可以从这里开始。 这是一篇关于创建combobox并用一些数据填充它的简单文章。

有许多方法可以执行此任务。 这是一个简单的:

              Left Docked StackPanel 2   Bottom Docked StackPanel Left      

接下来,我们有C#代码:

  private void myButton_Click(object sender, RoutedEventArgs e) { ComboBoxItem cboBoxItem = new ComboBoxItem(); // Create example instance of our desired type. Type type1 = cboBoxItem.GetType(); object cboBoxItemInstance = Activator.CreateInstance(type1); // Construct an instance of that type. List myStrings = new List(); // Fill a list with useful strings. for (int i = 0; i < 12; i++) { string newName = "stringExample" + i.ToString(); myStrings.Add(newName); } foreach (string str in myStrings) // Generate the objects from our list of strings. { ComboBoxItem item = this.CreateComboBoxItem((ComboBoxItem)cboBoxItemInstance, "nameExample_" + str, str); cboBox1.Items.Add(item); // Add each newly constructed item to our NAMED combobox. } } private ComboBoxItem CreateComboBoxItem(ComboBoxItem myCbo, string content, string name) { Type type1 = myCbo.GetType(); ComboBoxItem instance = (ComboBoxItem)Activator.CreateInstance(type1); // Here, we're using reflection to get and set the properties of the type. PropertyInfo Content = instance.GetType().GetProperty("Content", BindingFlags.Public | BindingFlags.Instance); PropertyInfo Name = instance.GetType().GetProperty("Name", BindingFlags.Public | BindingFlags.Instance); this.SetProperty(Content, instance, content); this.SetProperty(Name, instance, name); return instance; //PropertyInfo prop = type.GetProperties(rb1); } 

注意:这是使用reflection。 如果您想了解有关reflection基础知识的更多信息以及您可能想要使用它的原因,这是一篇很棒的介绍性文章:

  • .NET中的反思简介

如果您想了解更多关于如何在WPF中使用reflection的信息,请参考以下资源:

  • 对WPF Rockstars的反思
  • 使用reflection的力量来创建和操纵托管对象

如果你想大规模加速reflection的性能,最好使用IL来做到这一点,如下所示:

  • 使用IL的ActivatorCreateInstance方法的快速版本

  • 快速动态属性和字段访问器

我认为comboBox1.Items.Add("X");string添加到ComboBox,而不是ComboBoxItem

正确的解决方案是

 ComboBoxItem item = new ComboBoxItem(); item.Content = "A"; comboBox1.Items.Add(item); 

使用OleDBConnection – >连接到Oracle

 OleDbConnection con = new OleDbConnection(); con.ConnectionString = "Provider=MSDAORA;Data Source=oracle;Persist Security Info=True;User ID=system;Password=**********;Unicode=True"; OleDbCommand comd1 = new OleDbCommand("select name from table", con); OleDbDataReader DR = comd1.ExecuteReader(); while (DR.Read()) { comboBox_delete.Items.Add(DR[0]); } con.Close(); 

就这样 :)