2

I have several comboboxes which are initialiced with a default text which will be replaced later on by textes out of a text-file to support different languages. I tried different ways to change the text but none of them worked out:

Initialization:

myCombobox.itemSource = new ObservableCollection(Of String){"FirstItem", "SecoundItem"})

First Way:

myCombobox.Items(i) = GetString(myCombobox.Items(i))

Secound Way:

Dim comboboxStr = myCombobox.Items(i)
myCombobox.Items.RemoveAt(i)
myCombobox.Items.Add(GetString(i))

Both ways throw an InvalidOperationException with the hint to try "ItemsControl.ItemsSource" instead of "ItemsSource"

Is there maybe another way to change the items or what why is this exception occurring?

e_card
  • 119
  • 2
  • 10

1 Answers1

1

This is a common problem. Once you have data bound the ItemsControl.ItemsSource (or any class derived from ItemsControl, then you cannot use the ItemsControl.Items property to manipulate the data. Instead, (you should declare a property and) just access the data collection that you data bound directly:

SomeProperty = new ObservableCollection(Of String){"FirstItem", "SecoundItem"})

...

myCombobox.itemSource = SomeProperty

...

SomeProperty.Remove(someItem)
Sheridan
  • 68,826
  • 24
  • 143
  • 183