Event ObservableCollection and CollectionChanged

Why is the assembly event not triggered in the following code, but can I see a new InventoryBTO instance that I am adding to the ObservableCollection?

 private ObservableCollection<InventoryBTO> _inventoryRecords;
    public ObservableCollection<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);
        }

        _inventoryRecords = InventoryListBTO.GetAllInventoryRecords();
    }

    void InventoryRecords_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {

    } 
+5
source share
2 answers

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)
{
    //e.NewItems will be an IList of all the items that were added in the AddRange method...
} 
+10

public ObservableCollection<InventoryBTO> InventoryRecords
{
    get { return _inventoryRecords; }
    set 
    { 
      _inventoryRecords = value; 
      onPropertyChanged(this, "InventoryRecords");

    }
 }

public ObservableCollection<InventoryBTO> InventoryRecords
{
    get { return _inventoryRecords; }
    set 
    { 
      _inventoryRecords = value; 
      OnPropertyChanged(new PropertyChangedEventArgs("InventoryRecords"));

    }
 }

.

0

All Articles