The project I am working on requires all controls to be added to the screen programmatically.
I have tried creating a data grid with 2 columns one of which has an combo box edit template.
The code below I copied from other forum posts and modified it to suit my own needs however when I try to edit a record/cell the combo box is not displayed at all.
Does anyone know why or have any suggestions?
Also i read somewhere that is not recommended to create this programmatically is this true? If it is true what are the risks and implications of doing it programmatically?
this.xceedDataGrid.AutoCreateColumns = false;
this.xceedDataGrid.EditTriggers = EditTriggers.CellIsCurrent;
this.xceedDataGrid.CellEditorDisplayConditions = CellEditorDisplayConditions.MouseOverCell;
Column titleCol = new Column("Title", "Title", new Binding("Title"));
List<String> itemList = new List<String>();
ObjectQuery<Course> courseQuery = schoolContext.Course;
foreach (Course currentCourse in courseQuery)
{
itemList.Add(currentCourse.Title.ToString());
}
Binding binding = new Binding();
binding.Source = itemList;
FrameworkElementFactory factory = new FrameworkElementFactory(typeof(ComboBox));
factory.SetValue(ComboBox.ItemsSourceProperty, binding);
CellEditorBindingExtension cellEditorBindingExtension = new CellEditorBindingExtension();
factory.SetValue(ComboBox.SelectedValueProperty, cellEditorBindingExtension.ProvideValue(null) as BindingBase);
DataTemplate editTemplate = new DataTemplate(typeof(ComboBox));
editTemplate.VisualTree = factory;
CellEditor cellEditor = new CellEditor();
cellEditor.EditTemplate = editTemplate;
titleCol.CellEditor = cellEditor;
this.xceedDataGrid.Columns.Add(titleCol);
this.xceedDataGrid.Columns.Add(new Column("OnlineCourse", "OnlineCourse", new Binding("OnlineCourse.URL")));