I have a datagrid and in the DataGrid.AutoGeneratingColumn
Event I change some of the columns to DataGridComboBoxColumns.
At the same time I also want to add a selection changed event to that new combobox, but can't figure out how to get access to the combobox control within the DataGridComboBoxColumn.
private void dgGrid_AutogeneratingColumns(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
var cb = new DataGridComboBoxColumn();
switch (e.PropertyName)
{
case "name":
using (Entities context = new Entities())
{
List<object> fNames = (from x in context.view
select new object {objectname = x.objectname}).ToList<object>();
cb.Header = "xxx";
cb.ItemsSource = xxx;
cb.SelectedItemBinding = new Binding("xxx");
e.Column = cb;
//Get reference to combobox in this new column
//Add event to it
//?????
}
break;
//more ....
}
}
You cannot access ComboBox instance from DataGrid.AutoGeneratingColumn event handler, because ComBox is created only when a cell from that column goes into edit mode and new ComboBox instance is created every time that cell goes into edit mode.
Here is a sample code how to do what you want via DataGridComboBoxColumn.EditingElementStyle property by setting EventSetter for Selector.SelectionChangedEvent (ComboBox inherits from Selector):