【发布时间】:2015-09-08 08:51:27
【问题描述】:
我想创建一个加入Table 和ObservableCollection 功能的类。于是我写了:
public sealed class ObservableTable<T> : ObservableCollection<T>, ITable<T> where T : class
{
private Table<T> _table;
public Expression Expression => _table.AsQueryable().Expression;
public Type ElementType => _table.AsQueryable().ElementType;
public IQueryProvider Provider => _table.AsQueryable().Provider;
public new IEnumerable<T> Items => _table;
public new T this[int index] => _table.ElementAt(index);
public new int Count => _table.Count();
public ObservableTable(ref DataContext dbContext)
{
_table = dbContext.GetTable<T>();
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
public void InsertOnSubmit(T entity)
{
_table.InsertOnSubmit(entity);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add));
}
public void Attach(T entity)
{
_table.Attach(entity);
}
public void DeleteOnSubmit(T entity)
{
_table.DeleteOnSubmit(entity);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove));
}
}
但是尽管_table 对象正确地从数据库中获取了所有记录,但当我将我的类转换为ObservableCollection 时,集合是空的。我应该覆盖什么才能让它工作? Items 和 Count 属性还不够吗?
【问题讨论】:
标签: c# .net oop inheritance observablecollection