GridTemplateColumn with custom combobox on edit

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:

Cattura.PNG


2 Replies 1 reply marked as answer

EL Elisa June 30, 2022 08:34 AM UTC

I have set the SelectedValue property and now it works:


Binding valueBinding = new Binding(columnSchema.ColumnName);

colorComboboxFactory.SetBinding(ComboBox.SelectedValueProperty, valueBinding);


Marked as answer

VS Vijayarasan Sivanandham Syncfusion Team June 30, 2022 09:37 AM UTC

Hi Elisa,

We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you😊.


Regards,

Vijayarasan S


Loader.
Up arrow icon