8

To follow my previous post here => Binding SelectedItem of ComboBox in DataGrid with different type

I have now a datagrid containing 2 columns, one with a text, the other with a combobox (in a datatemplate, written thru the C# code, not the Xaml).

After having done some choice on the combobox, I now would like to parse the result but the value of the cell containing my combobox stay empty :

foreach(DataRowView row in Datagrid1.Items)
{
var firstColumNresult = row.Row.ItemArray[0];// Return correctly a string
var myrow = row.Row.ItemArray[1];// always empty... 
}

The result is that I cant get the values of my (previously generated) combobox.

I suppose one binding must missed somewhere...

This is the combobox creation code :

DataTable tableForDG = new DataTable();
tableForDG.Columns.Add(new DataColumn { ColumnName = "Name", Caption = "Name" });
tableForDG.Columns.Add(new DataColumn { ColumnName = "Attachment", Caption = "Attachment" }); // this column will be replaced
tableForDG.Columns.Add(new DataColumn { ColumnName = "AttachmentValue", Caption = "AttachmentValue" });
tableForDG.Columns.Add(new DataColumn { ColumnName = "DisplayCombo", Caption = "DisplayCombo", DataType=bool });


// Populate dataview
DataView myDataview = new DataView(tableForDG);
foreach (var value in listResults)// a list of string
{
DataRowView drv = myDataview.AddNew();
drv["Name"] = value.Name;
drv["Attachment"] = value.Name;// this column will be replaced...
drv["DisplayCombo"] = true;// but it can be false on my code...
}

var DG = myDataview;// 

 Datagrid1.ItemsSource = DG;
 Datagrid1.AutoGenerateColumns = true;
 Datagrid1.Items.Refresh();

 DataGridTemplateColumn dgTemplateColumn = new DataGridTemplateColumn();
 dgTemplateColumn.Header = "Attachment";
 var newCombobox = new FrameworkElementFactory(typeof(ComboBox));
 newCombobox.SetValue(ComboBox.NameProperty, "myCBB");

 Binding enableBinding = new Binding();

 newCombobox.SetValue(ComboBox.IsEnabledProperty, new Binding("DisplayCombo")); 
 newCombobox.SetValue(ComboBox.SelectedValueProperty, new Binding("AttachmentValue"));

 List<string> listUnitAlreadyAttached = new List<string>();
 // fill the list...

 enableBinding.Source = listUnitAlreadyAttached;
 newCombobox.SetBinding(ComboBox.ItemsSourceProperty, enableBinding);

 var dataTplT = new DataTemplate();
 dataTplT.VisualTree = newCombobox;
 dgTemplateColumn.CellTemplate = dataTplT;

 Datagrid1.Columns[1] = dgTemplateColumn;

Any idea/advice ?

PetersLast
  • 157
  • 3
  • 16
  • 1
    why did you do that this way? I mean creating elements in C# instead of XAML? – FoggyFinder Aug 28 '17 at 09:55
  • 1
    I don't understand how you get to the point of *always null* inside the loop. For me, `datagrid1.Items` is not a collection of `DataRowView`, so it will throw earlier. – grek40 Aug 28 '17 at 10:11
  • Foggy => I have a dynamic creation of columns first, with CheckBox checked or not inside, and differents column name, this is why I have chosen this solution – PetersLast Aug 28 '17 at 11:35
  • grek40 => row is not empty, but row.Row.ItemArray[1] is => which represents the generated comboBox. – PetersLast Aug 28 '17 at 11:36
  • It is like the selected value of my combobox was stored nowhere... – PetersLast Aug 28 '17 at 11:37
  • *\*Hint\** we don't know what `DG` is, that's why I had to get creative and create some random data and as a result, my items didn't behave like yours (as commented). So you better include a sample set of data for `DG` if you need help. – grek40 Aug 28 '17 at 12:39
  • Few questions: what is `strucTextValue`, what is the target for `Binding("AttachmentValue")`, I don't see `AttachmentValue` anywhere in the data? – grek40 Aug 28 '17 at 13:57
  • grek40: forget about the structTextValue, it does not matter actually. The AttachmentValue was a column where I wanted to store the selected value of my combobox. I added it to my dataTable. – PetersLast Aug 28 '17 at 14:03

1 Answers1

3

You should explicitely specify the binding mode and update trigger of your binding. Also use SetBinding instead of SetValue:

var valueBinding = new Binding("AttachmentValue")
{
    Mode = BindingMode.TwoWay,
    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
};
newCombobox.SetBinding(ComboBox.SelectedValueProperty, valueBinding);

This should enable you to get the selected value into your row data. It might not update in the displayed datagrid value for the AttachmentValue column.

grek40
  • 12,159
  • 1
  • 18
  • 46