Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A better way of forcing data bound WPF ListBox to update?

I have WPF ListBox which is bound to a ObservableCollection, when the collection changes, all items update their position.

The new position is stored in the collection but the UI does not update. So I added the following:

    void scenarioItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)     {         ToolboxListItem.UpdatePositions();         lstScenario.ItemsSource = null;         lstScenario.ItemsSource = ToolboxListItem.ScenarioItems;         this.lstScenario.SelectedIndex = e.NewStartingIndex;     } 

By setting the ItemsSource to null and then binding it again, the UI is updated,

but this is probably very bad coding :p

Suggestions?

like image 527
TimothyP Avatar asked Oct 31 '08 10:10

TimothyP


1 Answers

I have a Listbox bound to an object property which is of type List<MyCustomType>() and I verified that the following code updates the listbox when the List is updated.

void On_MyObjProperty_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {    MyListBox.Items.Refresh(); } 

If you're still facing issues, scan the VS IDE output window (Ctrl+W, O) and see if you can spot any binding errors reported.

like image 124
Gishu Avatar answered Sep 30 '22 01:09

Gishu