In my datagrid
, one of columns is DataGridComboBoxColumn
, where I'm trying to display different drop down menu in every row. Easy task when creating combo box in XAML instead of doing it programatically. My problem is that I have no idea how to bind it properly. This is what I tried:
private DataGridComboBoxColumn CreateComboValueColumn(List<Elements> elements)
{
DataGridComboBoxColumn column = new DataGridComboBoxColumn();
column.ItemsSource = elements;
column.DisplayMemberPath = "Text";
column.SelectedValuePath = "ID";
column.SelectedValueBinding = new Binding("Value");
return column;
}
public class Elements
{
public string Name { get; set; }
public string Value { get; set; }
public string Comment { get; set; }
public List<ComboItem> ComboItems { get; set; }
}
public class ComboItem
{
public string ID { get; set; }
public string Text { get; set; }
}
It seems that adding binding from style
works better than direct approach. This works:
private DataGridComboBoxColumn CreateComboValueColumn(List<Elements> elements)
{
DataGridComboBoxColumn column = new DataGridComboBoxColumn();
Style style = new Style(typeof(ComboBox));
//set itemsource = {Binding ComboItems}
style.Setters.Add(new Setter(ComboBox.ItemsSourceProperty, new Binding("ComboItems")));
column.DisplayMemberPath = "Text";
column.SelectedValuePath = "ID";
column.SelectedValueBinding = new Binding("Value");
column.ElementStyle = style;
column.EditingElementStyle = style;
return column;
}