I have an interesting problem with the Silverlight DataForm and child collections. I've also got a solution that works but it feels like a hack (it is a hack) and I was wondering if anyone had a more elegant solution.
I've got a DataForm bound to a collection of GroupViewModels. Each GroupViewModel has a ObservableCollection of UserViewModels which I'd like the user to be able to add to and delete from when the EditTemplate of the DataForm is shown.
The UI for doing that is a simple ComboBox with a list of all Users and a ListBox of the users currently in the group. There is an add and a delete button to move a User from one list to the other.
Now,...the view model that the DataForm is bound to implements IEditableObject,..which is cool because I get rollback behaviour if an edit is cancelled but in this case it's not quite enough. I need a way of telling the DataForm whether my VM is in a dirty state or not explicitly.
Since my ViewModel has a SelectedUserToAdd property which the SelectedItem of the all users ComboBox is bound to, when the user selects a user to add, the PropertyChanged event fires on my VM and the DataForm thinks that something has changed and the commit (OK) button lights up. It also lights up when the selection is changed in the ListBox because of the SelectedUserToRemove property.
Obviously what I want is for the commit button to light up if the UserViewModels in the ListBox have changed.
The solution is straight forward. I can stop the commit button lighting up if I remove change notification from the SelectedUserToAdd and SelectedUserToRemove properties and I can get the commit button to light back up by using a dummy property called 'Dirty' which raises NotifyPropertyChanged when either the AddUser or DeleteUser command gets fired.
As I say,..it works but it feels wrong. Anyone got a better idea?
Much thanks in advance.