0

In my project I have a data grid with three combo box template columns and are data bound to three collections and these columns are editable. The grid is bound to an observable collection, which is used to add new row to the grid like MyCollection.add(new obj("","","")). I tried the foreach method to get values in each template column in each row.But it is not working. Can anyone suggest me a way to loop through and get text in data grid containing template columns

Sony
  • 7,136
  • 5
  • 45
  • 68

1 Answers1

1

Since everything is data-bound, the best way would be to iterate through the MyCollection instead.

The DataGrid's cell content should really only be a representation of the data in those collections anyway, and it keeps your UI layer thinner.

slimbofat
  • 391
  • 2
  • 7
  • but the values are empty strings – Sony Jul 11 '15 at 18:57
  • If you're binding your comboboxes to properties on these objects in the collection, (which would be the way to go) then they wouldn't just have empty strings. You want to have the combobox bind its `SelectedItem` or `SelectedIndex` to a property on the items. That way, the items contain all of the information that you need. – slimbofat Jul 11 '15 at 19:55
  • Like this?`SelectedItem="{Binding MainDataCollection[0].Question, Mode = TwoWay}"` or can you tell me how – Sony Jul 11 '15 at 20:00
  • More simply, `SelectedIndex="{Binding ItemIndex}"` since the combobox's DataContext is already the item that the row is for. Add a new int property on the items in your MainDataCollection called `ItemIndex` to store the value, and you should be good to go. – slimbofat Jul 11 '15 at 20:06
  • i've got another problem too, and i've posted another question and i've posted the xaml,can you have a look at it http://stackoverflow.com/questions/31361282/selection-change-in-wpf-template-combo-box-column-changes-values-in-other-rows – Sony Jul 11 '15 at 20:12