I have a WPF containing a dynamically created DataGridComboBoxColumn this has a comboBox that I can change the value in any row. The table also contains a checkbox in another column, if I change this it calls a method each time a row is changed, I want the same result when I change the value in the combobox
I have a List of my Rum objects that for this purpose have three methods
public int rumStyle { get; set; }
public string rumStyleDesc { get; set; } // string representation of ruymStyle
public bool rumIsValid { get; set; }
This checkbox part works
DataGridTemplateColumn textColumnValid = new DataGridTemplateColumn();
textColumnValid.Header = headerRumIsValid;
FrameworkElementFactory checkBoxFactory = new FrameworkElementFactory(typeof(CheckBox));
Binding rumIsValidBinding = new Binding("rumIsValid");
checkBoxFactory.SetValue(CheckBox.IsCheckedProperty, rumIsValidBinding);
checkBoxFactory.AddHandler(CheckBox.ClickEvent, new RoutedEventHandler(LostFocus));
DataTemplate rumIsdValidTemplate = new DataTemplate();
rumIsdValidTemplate.VisualTree = checkBoxFactory;
textColumnValid.CellTemplate = rumIsdValidTemplate;
_dgData.Columns.Add(textColumnValid);
This part does display a combobox in each row but I want it to call the LostFocus (or another method) when the selection is changed.
DataGridComboBoxColumn comb = new DataGridComboBoxColumn();
comb.Header = headerRumStyle;
comb.SelectedValueBinding = new Binding("rumStyleDesc");
comb.ItemsSource = _rumStyles.getRumStyleNames();
FrameworkElementFactory comboFactory = new FrameworkElementFactory(typeof(ComboBoxItem));
comboFactory.SetValue(ComboBox.SelectedItemProperty, new Binding("rumStyleDesc"));
comboFactory.AddHandler(ComboBox.SelectionChangedEvent,
new SelectionChangedEventHandler(comboBox1_SelectionChanged));
DataTemplate comboboxTemplate = new DataTemplate();
comboboxTemplate.VisualTree = buttonFactory;
// How do I do this part
// comb.CellTemplate = comboboxTemplate;
_dgData.Columns.Add(comb);
A second but less important question is can I display the rumStlyeDesc in the combobox but read back the rumstyleId somehow.
Eventually found how to do it.
Firstly I added the list of RumStyles to each of my rums with a method to get the style names as a List
I then came up with the following code to build the ComboBox in a DataGridTemplateColumn not a DataGridComboBoxColumn
There is one issue remaining and that may be fixed by changing the SelectionChangedEvent that is that when ever the row is first displayed the comboBox1_SelectionChanged is called.