I have a class which has an ObservableCollection called Items. That list should fill up a RadGridView. Even though the OC contains data, the list stays empty, and after a bit of debuggingg I noticed it has some odd behavior. I have a breakpoint in the Get and Set of the property. First it hits the Get. Then the Set, but it never hits the Get again. Shouldn't the NotifyChanged also trigger the get after that then, so it updates the list in the view?
Here is the code below of the class I am talking about:
public class PagedCollection<TEntity> where TEntity : class, INotifyPropertyChanged
{
internal WorkflowEntities Context;
internal DbSet<TEntity> DbSet;
private ObservableCollection<TEntity> _items;
public ObservableCollection<TEntity> Items
{
get
{
return _items;
}
set
{
SetField(ref _items, value, nameof(Items));
}
}
public PagedCollection()
{
Context = new WorkflowEntities();
DbSet = Context.Set<TEntity>();
}
public virtual IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IQueryable<TEntity>> query = null,
string includeProperties = "")
{
IQueryable<TEntity> value = DbSet;
if (filter != null)
{
value = value.Where(filter);
}
value = includeProperties.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries).Aggregate(value, (current, includeProperty) => current.Include(includeProperty));
return query?.Invoke(value).ToList() != null ? query(value).ToList() : value.ToList();
}
// boiler-plate
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
protected bool SetField<T>(ref T field, T value, string propertyName)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}
}
The public virtual IEnumerable<TEntity> Get(...)
is being triggered by another class, which fills up the Items. Like this: PagedCollection.Items = PagedCollection.Get();
. This in turn fires the get, set, but not a get anymore, so my list stays empty in the view, even though there is data in PagedCollection.Items