ComboBox SelectedItem does not change after clearing the observable collection?

The problem is entirely caused by the Tables.Clear() line in your aaaa() method. Since Tables is an observable collection, wiping out all of the contents of the collection causes WPF to update the display with a new, empty list. Then it tries to select the currently active item using SelectedIndex which does not exist (because the list is now empty).

As a result, the binding engine is left with a value that cannot be applied, and decides to deactivate and detach the binding logic.

The problem is entirely caused by the Tables.Clear() line in your aaaa() method. Since Tables is an observable collection, wiping out all of the contents of the collection causes WPF to update the display with a new, empty list. Then it tries to select the currently active item using SelectedIndex, which does not exist (because the list is now empty).

As a result, the binding engine is left with a value that cannot be applied, and decides to deactivate and detach the binding logic: System.Windows. Data Warning: Got PropertyChanged event from Window1 for TheIndex System.Windows. Data Warning: GetValue at level 0 from Window1 using DependencyProperty(TheIndex): '1' System.Windows.

Data Warning: TransferValue - got raw value '1' System.Windows. Data Warning: TransferValue - using final value '1' System.Windows. Data Warning: Deactivate System.Windows.

Data Warning: Replace item at level 0 with {NullDataItem} System.Windows. Data Warning: Detach By the time it gets to the 'TheIndex = j;' line, the binding is no longer active and does not see the change to TheIndex, which means that desired index is no longer selected. There are a couple of solutions to solve this: Don't blow away the entire collection every time.

Without clearing the collection, the data binding logic always has an index to select, meaning it never detaches. Use a TwoWay binding. This works because now the ComboBox participates in binding; you clear Tables, the binding tries to set but can't find the index so the ComboBox resets to the special 'no index' position of -1, which then writes back to TheIndex (the two-way part), which is a valid value so the binding logic doesn't detach.

Select no index (-1) before clearing the collection. If no index (-1) is selected when Tables is cleared, then ComboBox doesn't try to apply SelectedItem, which means it doesn't 'see' the collection emptied and re-filled, and therefore, doesn't detach. Private void aaaa(object sender, RoutedEventArgs e) { TheIndex = -1; j = (j + 1)%10; Tables.Clear(); for (int I = 0; I ... More ways of debugging WPF bindings are here.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions