我有一个listbox tuningList
包含的元素List<Tuning> tunings
:
private void LoadList() {
foreach (Tuning tuning in tunings)
tuningList.Items.Add(tuning);
}
在应用程序的某个时刻,我想删除其中的一些元素tunings
并更新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
}
但在那之后列表框不会自行更新。
编辑:我没有通过 XAML 将列表框绑定到集合,我通过 LoadList() 添加了元素;
<ListBox x:Name="tuningList" Margin="8,0,8,152" Tap="tuningList_Tap"/>
编辑2:
<ListBox x:Name="tuningList" Margin="8,0,8,152" ItemsSource=tunings.Items Tap="tuningList_Tap"/>
编辑3:
<ListBox x:Name="tuningList" Margin="8,0,8,152" DataContext="{Binding RelativeSource={RelativeSource Self}}" ItemsSource="{Binding tunings}" Tap="tuningList_Tap"/>