Here is one way to do what you require, without having to connect alot of events from every single myObject. It goes a little outside the MVVM box, but despite people talking best practise and all, it does not ruin the application to deviate a little once in a while.
Personally, i prefer this method from filling up the object class.
The sample is made out of a messed up solution, so it might be a little difficult to make out some things, but i am sure you will get the idea.
XAML
<Window x:Class="WpfApplication1.Window2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vm="clr-namespace:WpfApplication1" Title="Window2" Height="300" Width="300"><Window.DataContext><vm:ViewModel/></Window.DataContext><Window.Resources><vm:Converter x:Key="SelectedCountConverter"/></Window.Resources><Grid><Grid.RowDefinitions><RowDefinition/><RowDefinition Height="auto"/></Grid.RowDefinitions><DataGrid ItemsSource="{Binding Objects}" AutoGenerateColumns="True" CurrentCellChanged="DataGrid_CurrentCellChanged_1"/><Label Content="{Binding Objects, Converter={StaticResource SelectedCountConverter}, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1"/></Grid></Window>
Window codebehind
public partial class Window2 : Window { public Window2() { InitializeComponent(); } private void DataGrid_CurrentCellChanged_1(object sender, EventArgs e) { (DataContext as ViewModel).NotifyPropertyChanged("Objects"); } }
ViewModel, Object & Converter
public class ViewModel : INotifyPropertyChanged { public ViewModel() { Objects = new List<myObject>(); for (int i = 0; i < 10; ++i) Objects.Add(new myObject { Selected = true }); } 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 event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(string Name) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(Name)); } } public class myObject { public bool Selected { get; set; } } public class Converter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return ((List<myObject>)value).Where(o => o.Selected).Count(); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return null; } }
Hope this helps.
Developing is part of being a developer.