c# - ObservableList ReadOny 数据网格

标签 c# wpf datagrid observablecollection readonly

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

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/

相关文章:

WPF DataGridTemplateColumn IsSelected ForgroundColor 未按预期工作

c# - 是否可以监听具有多个azure功能的服务总线?

C#/命令行界面 : Destructor not called if Dispose() used in it

wpf - 如何使用 Collections 构建 MVVM?

WPF DataGrid 在代码中更新其 ItemsSource 时不更新 UI

c# - 重新排序 DatagridViews 列并以编程方式保存新位置

c# - 使用int.TryParse C#时发生编译错误

c# - 如何从特定 session Identity Server 4、.Net Core 中注销用户?

c# - 转换器获取 DataRowView 而不是 DataGridCell

c# - WPF TextBox 数据绑定(bind)问题 > :-(