views:

50

answers:

1

I have a datagrid with two columns a and b. Column b represents a percentage.

I want to have a single editable row at the start, and have them fill in column a and b.

In the case that column b is less than 100%, I want to add a new editable row. I want to always add a new row when the total percentage is less than 100%.

Any ideas on how to do this (using MVVM)?

The datagrid is bound to an ObservableCollection.

A: 

First thing that comes to mind is to handle this completely in ViewModel. You could listen to CollectionChanged of your ObservableCollection, if an item is added, sum up column B, and if the sum is below 100%, add a new item to Observable-collection.

Same thing if you want to support editing of the existing items in the collection. Listen to PropertyChanged event on the items, and if Column B is changed, sum up column B and decide if a new item is needed.

Let me know if you want some sample code for this.

Tendlon
Thankyou very much Tendlon for your help.I was trying something very similar to that already but I was having issues with the propertychanged notification. I was attaching the event handler before adding the first item to the collection so it wasnt listening.Your post was enough to make me realise that my approach was right, and I was just missing a step. Thanks mate
Darren