将新项目添加到我的 ComboBox 导致 ItemsSource。例外
Add new item into my ComboBox lead to ItemsSource. Exception
我有 ComboBox
与我的 collection
绑定,现在我想在绑定之前添加另一个 Item
:"Please select..."
所以我试试这个:
combobox.Items.Add("Please select");
但是因为我有这个问题:
public ObservableCollection<MyData> collection { get; set; }
ItemsSource="{Binding collection}"
我收到的只是这个错误:
Items collection must be empty before using ItemsSource
那我该如何解决呢?
如果您要绑定到 ItemsSource,则不能手动更新组合框中的项目,因为这会使绑定无效。如果您想要 ItemsSource 中的另一个值,只需将其添加到 ViewModel 中的 "collection"。
我有 ComboBox
与我的 collection
绑定,现在我想在绑定之前添加另一个 Item
:"Please select..."
所以我试试这个:
combobox.Items.Add("Please select");
但是因为我有这个问题:
public ObservableCollection<MyData> collection { get; set; }
ItemsSource="{Binding collection}"
我收到的只是这个错误:
Items collection must be empty before using ItemsSource
那我该如何解决呢?
如果您要绑定到 ItemsSource,则不能手动更新组合框中的项目,因为这会使绑定无效。如果您想要 ItemsSource 中的另一个值,只需将其添加到 ViewModel 中的 "collection"。