wpf 在运行时向 ComboBox 添加项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21900332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:56:29  来源:igfitidea点击:

Add items to ComboBox at runtime?

c#wpfxamlcombobox

提问by herohuyongtao

I am trying to add items to a ComboBox(say Name="labelComboBox") at runtime when I pressed an add button (say with Name="add2labels" Click="add2labels_Click"). But the ComboBoxcannot show the values I newly added. What did I miss?

当我按下添加按钮(例如 with )时,我试图在运行时向 a ComboBox(例如Name="labelComboBox")添加项目Name="add2labels" Click="add2labels_Click"。但是ComboBox无法显示我新添加的值。我错过了什么?

The following is the event handler for the add button:

以下是添加按钮的事件处理程序:

private List<String> labels = new List<String>();
... ...
private void add2labels_Click(object sender, RoutedEventArgs e)
{
    labels.Add("new value");

    labelComboBox.ItemsSource = labels;
}

P.S. I am pretty sure the values were added to List<String> labelscorrectly (its count did increase each time).

PS我很确定这些值被List<String> labels正确添加(它的计数每次都增加)。



Updated with workable solutions (3 ways) :

更新了可行的解决方案(3 种方式):

  1. Use ObservableCollection(@AnatoliyNikolaev's answer).

    Change List<String> labelsto ObservableCollection<String> labels. And only need to call labelComboBox.ItemsSource = labels;once in all.

  2. Use Binding(@HarshanaNarangoda's answer).

    Add ItemsSource="{Binding Path=labels}"to ComboBox's properties.

  3. Use Refresh()(@EliranPe'er's anwer).

    Change the event handler to:

    ... ...
    labelComboBox.ItemsSource = labels;
    labelComboBox.Items.Refresh();      // new added
    
  1. 使用ObservableCollection(@AnatoliyNikolaev 的回答)。

    更改List<String> labelsObservableCollection<String> labels。并且只需要调用labelComboBox.ItemsSource = labels;一次。

  2. 使用Binding(@HarshanaNarangoda 的回答)。

    添加ItemsSource="{Binding Path=labels}"ComboBox的属性。

  3. 使用Refresh()(@EliranPe'er 的回答)。

    将事件处理程序更改为:

    ... ...
    labelComboBox.ItemsSource = labels;
    labelComboBox.Items.Refresh();      // new added
    

采纳答案by Anatoliy Nikolaev

You should use ObservableCollection<T>instead of List<String>:

你应该使用ObservableCollection<T>而不是List<String>

ObservableCollection represents a dynamicdata collection that provides notificationswhen items get added, removed, or when the whole list is refreshed.

ObservableCollection 表示一个动态数据集合,provides notifications当添加、删除项目或刷新整个列表时。

回答by Aftab Ahmed

Combobox has a display and value member to add values to combo-box you need to specify both.

Combobox 有一个 display 和 value 成员,可以将值添加到需要同时指定的组合框。

try this

尝试这个

ComboboxItem item = new ComboboxItem();
item.Text = "new value";
item.Value = 12;

labels.Items.Add(item);

回答by Harshana Narangoda

I think You have to change some code in XAML to following.You have to bind data to your Combo Box.

我认为您必须将 XAML 中的一些代码更改为以下代码。您必须将数据绑定到您的组合框。

<ComboBox ItemsSource="{Binding}" Height="23" HorizontalAlignment="Left" Name="comboBox1" />

回答by Eliran Pe'er

Try using labelComboBox.Items.Refresh();

尝试使用 labelComboBox.Items.Refresh();