bsh*_*52s 5 c# collections events bindinglist
我想在将项添加到BindingList之前进行一些处理.我看到有一个ListChanged事件,但在添加项目后会触发此事件.只有在调用AddNew方法(而不是Add方法)时才会触发AddingNew事件.以前有人做过这样的事吗?
更新:
我创建了以下类,当在IList上调用Add方法时,我的新Add方法被触发.那么,我是否有其他地方读过的铸造问题?如果我从集合中删除了ISpecialCollection接口,则不会调用我的Add方法.有人可以解释为什么它的行为不同吗?如果我使用ISpecialCollection <接口,是否会出现转换问题?
public interface ISpecialCollection<T> : IList<T>
{
}
public class SpecialCollection<T> : BindingList<T>, ISpecialCollection<T>
{
public new void Add (T item)
{
base.Add(item);
}
}
class Program
{
static void Main(string[] args)
{
IList<ItemType> list = new SpecialCollection<ItemType>();
list.Add(new ItemType());
}
}
Run Code Online (Sandbox Code Playgroud)