Sha*_*ead 7 c# silverlight xaml windows-phone-7
我正在使用带有自定义ItemsSource的ListBox:
this.ListOfPersonsListBox.ItemsSource = (List<Person>)ListOfPersons.AllPersons;
Run Code Online (Sandbox Code Playgroud)
ListOfPersons是一个静态类,因此它无法实现INotifyPropertyChanged和IObservableCollection.
更新列表后重绘ListBox的最简单方法是什么?我目前的代码有效,但我想找到一个更清洁的解决方案:
private void SyncButton_Click(object sender, EventArgs e)
{
ListOfPersons.Sync();
this.ListOfPersonsListBox.ItemsSource = null;
this.ListOfPersonsListBox.ItemsSource = ListOfPersons.AllPersons;
}
Run Code Online (Sandbox Code Playgroud)
考虑使用ObservableCollection而不是a List.它在INotifyPropertyChanged内部实施.您可以遍历列表并将每个元素添加到新ObservableCollection对象并将其绑定到ListBox.
如果你要经常转换,你可以创建一个 Extension method
public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> myList)
{
var oc = new ObservableCollection<T>();
foreach (var item in myList)
oc.Add(item);
return oc;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8298 次 |
| 最近记录: |