开发者

Binding to a List<object> in silverlight problem

开发者 https://www.devze.com 2022-12-24 04:54 出处:网络
Can someone suggest what I am doing wrong?Basically I have a List Items, when an item gets added to the list I am resetting the collection to the viewmodel p开发者_高级运维roperty.The only way I can g

Can someone suggest what I am doing wrong? Basically I have a List Items, when an item gets added to the list I am resetting the collection to the viewmodel p开发者_高级运维roperty. The only way I can get it to work is if I null the member object out before reassigning the binding. Any suggestions on how to go about updating a UI when an item gets added to a List?

    public List<Item> RegisteredItems 
    {
        get
        {
            return m_vRegisteredItems;
        }
        set
        {
            m_vRegisteredItems= null;
            NotifyPropertyChanged("RegisteredItems");
            m_vRegisteredItems= value;
            NotifyPropertyChanged("RegisteredItems");
        }
    }


Use an ObservableCollection<T> instead of a List<T>. ObservableCollection<T> implements the INotifyCollectionChanged interface which allows Silverlight to track changes to the collection.


Make sure your collection implements INotifyCollectionChanged. ObservableCollection<T> does this for you.

If you replace your List<Item> with ObservableCollection<Item> it will just work.

Also, you shouldn't "set" your list - you just need the getter, unless you'll be changing the entire list.

0

精彩评论

暂无评论...
验证码 换一张
取 消