0
我正在學習可觀察集合,所以我編寫了一個小程序來測試我的進度。 我有一個可觀察的集合類,我從列表中提供初始值並將observablecollection綁定到Datagrid。它工作得很好,但是當我使用myListOfPlayers.myList.Clear()清除列表時,Datagrid不清除。我認爲INotifyPropertyChanged屬性會處理。我究竟做錯了什麼?Observable collection collection已更改問題
public class PlayerList : ObservableCollection<PlayerName> //observable collection class
{
public PlayerList()
: base()
{
Clear();
foreach (var p in myListOfPlayers.myList.OrderBy(x => x.Score))
{
Add(p);
}
}
public PlayerList(List<PlayerName> list)
: base(list)
{
Clear();
foreach (var p in list)
{
Add(p);
}
}
}
我在PlayerName類執行INotifyPropertyChanged:
public class PlayerName : INotifyPropertyChanged
{
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("Name");
}
}
private int _score;
public int Score
{
get { return _score; }
set
{
_score = value;
OnPropertyChanged("Score");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
這很有道理,但是我該如何清除可觀察集合? – 2011-01-14 19:19:40