Notifying my UI to refresh when a collection in it's VM changes

I have a usercontrol bound to a VM. This VM contains a collection property, lets call it "MyCollection" and several regular properties lets call one of them "SomeProperty". As you can see, the get and set logic for this property references the collection in the VM.

The problem is, when I make a change to "MyCollection", this obviously has an impact on the values that are shown in the UI (as they are calculated based on it). However, my UI doesn't appear to be smart enough to update itself whenever "MyCollection" changes.

Here is the VM my usercontrol is bound to:

public class MyVM
{
    private ObservableCollection<SomeOtherVM> _myCollection = new ObservableCollection<SomeOtherVM>();

    public MyVM()
    {

    }

    public ObservableCollection<SomeOtherVM> MyCollection
    {
        get { return _myCollection; }
        [Notify]
        set 
        { 
            _myCollection = value;
        }
    }

    public virtual string SomeProperty
    {
        get
        {
            if (_myCollection.Count == 1)
                return _myCollection[0].SomeProperty;
            else
                return "More than one "SomeOtherVM" has been selected";
        }
        [Notify]
        set
        {
            foreach (SomeOtherVM s in _myCollection)
            {
                s.SomeProperty = value;
            }
        }
    }
}

}

Note that nothing in my usercontrol is directly bound to the collection, it is only bound to other properties that reference the collection in its get; set; methods.

Is there anything I could do in the VM to force the UI to update whenever "MyCollection" is changed? I want to avoid having to put anything in the code behind for the user control.

Answers


Subscribe to CollectionChanged of MyCollection and fire PropertyChanged-notifications for the other properties there (no fancy attribute usage for you).


Need Your Help

Javascript function to convert milliseconds yields strange results

javascript

I'm creating my own custom TimeObject class for a tiny web app I'm making.

Standalone replica sets mongodb

mongodb replication

I need to create a standalone replica set in mongo. I followed the steps here: http://docs.mongodb.org/manual/tutorial/convert-standalone-to-replica-set/

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.