gpt4 book ai didi

c# - ObservableList ReadOny 数据网格

转载 作者:太空宇宙 更新时间:2023-11-03 14:09:38 24 4
gpt4 key购买 nike

我创建了可观察列表的以下扩展来模拟只读行为。

public class WeirdList<T> : ObservableCollection<T>
{

public new void Add(T item)
{
throw new NotImplementedException("Add function not implemented.");
}

public new void Insert(int index, T item)
{
throw new NotImplementedException("Insert() not implemented.");
}

}

但是,如果我将其绑定(bind)到具有 CanUserAddRows=true 的数据网格,那么当我通过 GUI 在数据网格上添加一行时,不会抛出异常,但我可以看到该对象已添加到集合中,尽管已覆盖允许添加的明显方法。

如果我尝试添加代码,就会抛出异常。

有什么帮助吗?

问候

最佳答案

您没有覆盖函数,只是重新定义了它们。您需要使用 override 而不是 new。网格可能将数据源转换为 ICollection,这意味着您的函数不会被调用。仅当对象正是您的类型时才会使用它们。

如注释中所述,AddInsert 不是虚拟的,而是覆盖函数 InsertItem。 (添加和插入都在内部调用此函数)

关于c# - ObservableList ReadOny 数据网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8263134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com