The problem is that you are assigning your personal member to a new instance ObservableCollectionthat you are returning from your method. Therefore, what happens, you connect to the event of one collection, but then discard this instance and replace it with a new instance to which you have never connected an event handler. Here is what you can do. Create a class that inherits from ObservableCollectionand adds the addrange method:
public class RangeObservableCollection<T> : ObservableCollection<T>
{
private bool surpressEvents = false;
public void AddRange(IEnumerable<T> items)
{
surpressEvents = true;
foreach (var item in items)
{
base.Add(item);
}
this.surpressEvents = false;
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, items.ToList()));
}
protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (!this.surpressEvents)
{
base.OnCollectionChanged(e);
}
}
}
:
private RangeObservableCollection<InventoryBTO> _inventoryRecords;
public RangeObservableCollection<InventoryBTO> InventoryRecords
{
get { return _inventoryRecords; }
set { _inventoryRecords = value; }
}
private InventoryBTO _selectedRecord;
public InventoryBTO SelectedRecord
{
get { return _selectedRecord; }
set
{
if (_selectedRecord != value)
{
_selectedRecord = value;
OnPropertyChanged(new PropertyChangedEventArgs("SelectedRecord"));
}
}
}
public InventoryViewModel()
{
if (_inventoryRecords == null)
{
InventoryRecords = new ObservableCollection<InventoryBTO>();
this.InventoryRecords.CollectionChanged += new NotifyCollectionChangedEventHandler(InventoryRecords_CollectionChanged);
}
this.InventoryRecords.AddRange(InventoryListBTO.GetAllInventoryRecords());
}
void InventoryRecords_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
}