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 ListBox
control to an ObservableCollection<T>
, which will automatically notify the view when an item is added or removed due to the fact that it implements INotifyCollectionChanged
.
You need to bind the collection to the ListBox
. Instead of using LoadList
set the ItemsSource
in XAML. Otherwise nothing will happen.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.