Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 224
PetersLast Avatar asked Aug 24 '17 09:08

PetersLast


1 Answers

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.

like image 75
grek40 Avatar answered Oct 14 '22 20:10

grek40