的ObservableCollection和CollectionChanged事件 [英] ObservableCollection and CollectionChanged Event

查看:145
本文介绍了的ObservableCollection和CollectionChanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么collectionchanged事件不火在下面的代码,但我可以看到InventoryBTO的新实例我添加到的ObservableCollection?

 私人的ObservableCollection< InventoryBTO> _inventoryRecords; 
公众的ObservableCollection< InventoryBTO> InventoryRecords
{
{返回_inventoryRecords; }
集合{_inventoryRecords =价值; }
}

私人InventoryBTO _selectedRecord;
公共InventoryBTO SelectedRecord
{
{返回_selectedRecord; }

{
如果(!_selectedRecord =值)
{
_selectedRecord =价值;
OnPropertyChanged(新PropertyChangedEventArgs(SelectedRecord));
}
}
}

公共InventoryViewModel()
{
如果(_inventoryRecords == NULL)
{
InventoryRecords =新的ObservableCollection< InventoryBTO>();
this.InventoryRecords.CollectionChanged + =新NotifyCollectionChangedEventHandler(InventoryRecords_CollectionChanged);
}

_inventoryRecords = InventoryListBTO.GetAllInventoryRecords();
}

无效InventoryRecords_CollectionChanged(对象发件人,NotifyCollectionChangedEventArgs E)
{

}


解决方案

的问题是,你在你的分配私有成员到的ObservableCollection <的新实例/ code>你得到从你的方法了。因此,发生的事情是,你挂接到一个集合的事件,但随后吹走的实例,并用你从来没有迷上了事件处理程序,以一个新的实例替换它。这里是你可以做什么。创建从的ObservableCollection 继承,并增加了一个的AddRange方法的类:

 公共类RangeObservableCollection< T> :与的ObservableCollection LT; T> 
{
私人布尔surpressEvents = FALSE;

公共无效的AddRange(IEnumerable的< T>项目)
{
surpressEvents = TRUE;
的foreach(在项目VAR项目)
{
base.Add(项目);
}
this.surpressEvents = FALSE;
this.OnCollectionChanged(新NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add,items.ToList()));

}

保护覆盖无效OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs E)
{
如果(!this.surpressEvents)
{
base.OnCollectionChanged(E);
}
}
}



然后,你可以改变你类这样的:

 私人RangeObservableCollection< InventoryBTO> _inventoryRecords; 
公共RangeObservableCollection< InventoryBTO> InventoryRecords
{
{返回_inventoryRecords; }
集合{_inventoryRecords =价值; }
}

私人InventoryBTO _selectedRecord;
公共InventoryBTO SelectedRecord
{
{返回_selectedRecord; }

{
如果(!_selectedRecord =值)
{
_selectedRecord =价值;
OnPropertyChanged(新PropertyChangedEventArgs(SelectedRecord));
}
}
}

公共InventoryViewModel()
{
如果(_inventoryRecords == NULL)
{
InventoryRecords =新的ObservableCollection< InventoryBTO>();
this.InventoryRecords.CollectionChanged + =新NotifyCollectionChangedEventHandler(InventoryRecords_CollectionChanged);
}

this.InventoryRecords.AddRange(InventoryListBTO.GetAllInventoryRecords());
}

无效InventoryRecords_CollectionChanged(对象发件人,NotifyCollectionChangedEventArgs E)
{
//e.NewItems将全部在的AddRange添加的项目的一个IList方法...
}


Why does the collectionchanged event not fire in the following code, yet I can see the new instance of InventoryBTO I add 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)
    {

    } 

解决方案

The problem is that you're assigning your private member to a new instance of an ObservableCollection that you're getting back from your method. Therefore, what's happening is, your hooking up to the event of one collection, but then blowing away that instance and replacing it with a new instance you never hooked up an event handler to. Here's what you can do. Create a class that inherits from ObservableCollection and adds an 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);
        }
    }
}

Then, you can change your class to this:

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...
} 

这篇关于的ObservableCollection和CollectionChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆