【问题标题】:Add new item into my ComboBox lead to ItemsSource. Exception将新项目添加到我的 ComboBox 导致 ItemsSource。例外
【发布时间】:2015-08-02 12:46:50
【问题描述】:

我有ComboBox 与我的collection 绑定,现在我想在绑定之前添加另一个Item"Please select..." 所以我试试这个:

combobox.Items.Add("Please select");

但是因为我有这个叮当声:

public ObservableCollection<MyData> collection { get; set; }

ItemsSource="{Binding collection}"

我收到的只是这个错误:

Items 集合在使用 ItemsSource 之前必须为空

那我该如何解决呢?

【问题讨论】:

  • 你用谷歌搜索了吗?您的问题有很多可能的解决方案。
  • 看看this answer

标签: wpf binding


【解决方案1】:

如果您要绑定到 ItemsSource,则不能手动更新组合框中的项目,因为这会使绑定无效。如果您想要 ItemsSource 中的其他值,只需将其添加到 ViewModel 中的“集合”中即可。

【讨论】:

    猜你喜欢
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 2014-07-09
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多