How to get value of a programmatically written combobox in a datagrid in wpf?
Asked Answered
F

1

7

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 ?

Flatfoot answered 24/8, 2017 at 9:12 Comment(8)
why did you do that this way? I mean creating elements in C# instead of XAML?Lithuanian
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.Hoffman
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 solutionFlatfoot
grek40 => row is not empty, but row.Row.ItemArray[1] is => which represents the generated comboBox.Flatfoot
It is like the selected value of my combobox was stored nowhere...Flatfoot
*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.Hoffman
Few questions: what is strucTextValue, what is the target for Binding("AttachmentValue"), I don't see AttachmentValue anywhere in the data?Hoffman
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.Flatfoot
H
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.

Hoffman answered 28/8, 2017 at 14:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.