Quantcast
Channel: MVVM and Updating a Selected Count
Viewing all articles
Browse latest Browse all 4

MVVM and Updating a Selected Count

$
0
0

I have a viewmodel that has a list of objects and on the object i have a selected property.  My view has a datagrid and the itemssource of the datagrid is the list of objects in the viewmodel.  The datagrid has a checkbox column and this is bound to the selected property of my object, so when the checkbox is marked the selected property = true and when its unchecked selected = false.  When the selected property gets updated I want to fire an event that updates a property in my viewmodel that tracks a count of selected items.  What's the best way to do this?  Through .net events, handle the click checkbox event in the code behind which would require me to reference my viewmodel from the view but i could abstract it to an interface, pass a delegate into my object to update the selected count when the property changes?

public class ViewModel
{
        private List<myObject> _objects;
        public List<myObject> Objects
        {
            get { return _objects; }
            set
            {
                _objects = value;
                NotifyPropertyChanged("Objects");
            }
        }
        private int _count;
        public int Count        
        {
            get { return _count; }
            set
            {
                _count = value;
                NotifyPropertyChanged("Count");
            }
        }
}
public class myObject
{
        private bool _selected;
        public bool Selected        
        {
            get { return _selected; }
            set
            {
                bool changed = false;
                if(_selected != value)
                {
                         changed = true;
                } 
                _selected = value;
                NotifyPropertyChanged("Selected");
                if(changed)
                {
                         SelectedChanged();
                }               
            }
        }
}


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images