I have a listbox tuningList containing elements of the List<Tuning> tunings:
private void LoadList() {
foreach (Tuning tuning in tunings)
tuningList.Items.Add(tuning);
}
At certain point in the app I want to delete some elements in the tunings and update the listbox
int selectionIndex = tuningList.SelectedIndex;
if (selectionIndex >= 0) {
pageTitle.Text = "Deleted tuning";
tunings.RemoveAt(selectionIndex);
// tuningList.Items.RemoveAt(selectionIndex);
saveData(saver); //saves data to isolated storage
}
but after that listbox does not update itself.
EDIT: I did not bind the listbox to the collection via XAML, I added the elements by the LoadList();
<ListBox x:Name="tuningList" Margin="8,0,8,152" Tap="tuningList_Tap"/>
EDIT2:
<ListBox x:Name="tuningList" Margin="8,0,8,152" ItemsSource=tunings.Items Tap="tuningList_Tap"/>
EDIT3:
<ListBox x:Name="tuningList" Margin="8,0,8,152" DataContext="{Binding RelativeSource={RelativeSource Self}}" ItemsSource="{Binding tunings}" Tap="tuningList_Tap"/>
My piece of advice in this case would be binding your
ListBoxcontrol to anObservableCollection<T>, which will automatically notify the view when an item is added or removed due to the fact that it implementsINotifyCollectionChanged.You need to bind the collection to the
ListBox. Instead of usingLoadListset theItemsSourcein XAML. Otherwise nothing will happen.