I build a DataGrid the contains a custom ComboBoxColumn that shows and selects the proper fields when I select it with a mouse, but resets to the first item in the list after the ComboBox loses focus. Additionally, when when I import data from an Excel spreadsheet, the ComboBox does not change to match the value in the worksheet.
Note: I'm planning on reusing this class to create a series of ComboBoxes for different data.
What is preventing the Combo Box from accepting a new value?
ComboList class
namespace x.Models
{
public class ComboList
{
public int FieldID {get;set;}
public string FieldString {get;set;}
}
}
XAML Combo Box code
<DataGridComboBoxColumn Header="Platform Type" x:Name="PlatformTable" SelectedValueBinding="{Binding FieldID}" DisplayMemberPath="FieldString" SelectedValuePath="1" />
Code(behind)
public partial class MainWindow: Window
{
public ObservableCollection<Models.ComboList> PlatformCombo {get;set;}
public MainWindow()
{
PlatformCombo = new ObservableCollection<Models.ComboList>()
{
new Models.ComboList() {FieldID=1,FieldString="Mounted"},
new Models.ComboList() {FieldID=2,FieldString="Dismounted"}
};
PlatformTable.ItemsSource = PlatformCombo;
}
Sample Excel data
Platform Type
1
2
The XAML Combo Box SelectedValueBinding
must be set to the DataGrid Combo Box Control Name instead of the generic Combo Box FieldID
and the UpdateSourceTrigger
needs to be set to LostFocus
.
SelectedValueBinding="{Binding Platform_Type, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"