I have a dynamic column that contains a custom combobox. The combobox and its ItemsSource are displayed correctly, but the cell value is not changed. The cell needs to contain selected value which is the Id field of the itemssource.
How to say that the cell binds this value?
Code behind:
//create column
var colorComboboxColumn = new GridTemplateColumn();
FrameworkElementFactory colorComboboxFactory = new FrameworkElementFactory(typeof(ComboBox));
colorComboboxColumn.MappingName = columnSchema.ColumnName;
colorComboboxColumn.HeaderText = columnSchema.ColumnDesc;
//assign itemsource
var itemSource = new List<ItemSp>();
itemSource.Add(new ItemSp { Id = 1, Value = "#ff0000", Description = "Red" });
itemSource.Add(new ItemSp { Id = 2, Value = "#00ff00", Description = "Green" });
itemSource.Add(new ItemSp { Id = 3, Value = "#0000ff", Description = "Blue" });
colorComboboxFactory.SetValue(ComboBox.ItemsSourceProperty, itemSource);
//create combobox itemstemplate
var comboboxTemplate = Application.Current.FindResource("colorComboboxItemsTemplate") as DataTemplate;
colorComboboxFactory.SetValue(ComboBox.ItemTemplateProperty, comboboxTemplate);
//set
SelectedValuePath of combobox
colorComboboxFactory.SetValue(ComboBox.SelectedValuePathProperty, "Id");
//assign template to CellTemplate
DataTemplate template = new DataTemplate();
template.VisualTree = colorComboboxFactory;
colorComboboxColumn.CellTemplate = template;
Template:
<DataTemplate x:Key="colorComboboxItemsTemplate">
<StackPanel Orientation="Horizontal">
<Rectangle Fill="{Binding Value}" Width="16" Height="16" Margin="0,2,5,2" />
<TextBlock Text="{Binding Description}" Width="60" />
</StackPanel>
</DataTemplate>
Result: