[英]How to refresh listbox in the windows Phone
我有一個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"/>
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"/>
在這種情況下,我的建議是將ListBox
控件綁定到ObservableCollection<T>
,由於該項目實現了INotifyCollectionChanged
,因此在添加或刪除項目時,它將自動通知視圖。
您需要將集合綁定到ListBox
。 而不是使用LoadList
而是在XAML中設置ItemsSource
。 否則,將不會發生任何事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.