我有一个列表/事件,通过以下代码
class MyList<T> : List<T>
{
public event EventHandler OnAdd;
public void Add(T item)
{
if (null != OnAdd)
{
OnAdd(this, null);
}
base.Add(item);
}
}
MyList<connectedUser> myListUsers = new MyList<connectedUser>();
myListUsers.OnAdd += new EventHandler (myListUsers_OnAdd );
private void myListUsers_OnAdd(object sender, EventArgs e)
{
}
public class connectedUser
{
public string userID { get; set; }
public string marketID { get; set; }
}
在我的OnAdd事件(myListUsers_OnAdd)中,我希望我的插入对象作为参数,换句话说我怎样才能在事件中读取插入的对象
答案 0 :(得分:2)
你应该使用ObservableCollection,这已经是一个在添加或删除对象时被调用的事件。
示例:强>
var collection = new ObservableCollection<int>();
collection.CollectionChanged += CollectionOnCollectionChanged;
,你的方法如下:
private static void CollectionOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs) {
//notifyCollectionChangedEventArgs.NewItems <= this are the items that got added
//notifyCollectionChangedEventArgs.OldItems <= this are the items that got removed
}