How to make TwoWay binding on properties of ObservableCollection of custom class using mvvm pattern?

Posted by mill on Stack Overflow See other posts from Stack Overflow or by mill
Published on 2011-01-04T22:34:55Z Indexed on 2011/01/04 23:53 UTC
Read the original article Hit count: 256

Filed under:

I have the following class:

public class UserGroup { public string GroupName { get; set; } public bool IsIntheGroup{ get; set; } }

I want to bind an ObservableCollection of UserGroup items to listbox containing checkbox’s for each item in the collection and the checkbox is cheked based on the IsIntheGroup property of the UserGroup. In my ViewModel I made an ObservableCollection of the UserGroup class:

public ObservableCollection Groups { get; set; }

and loaded its contents (instances of UserGroup) from my database model

I used the following code in my view:

ListBox ItemsSource="{Binding Groups, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"> ListBox.ItemTemplate> DataTemplate> StackPanel Orientation="Horizontal"> CheckBox IsChecked="{Binding IsIntheGroup, Mode=TwoWay}"/> TextBlock Text="{Binding GroupName}" /> /StackPanel> /DataTemplate> /ListBox.ItemTemplate> /ListBox>

The problem is I am not notified when the user checks/unchecks a check box in the list so my two way binding failed…

How do I do a two way binding in such a case?

© Stack Overflow or respective owner

Related posts about wpf