3

I'm using Datagrid in WPF and DataGridComboBoxColumn. Please find the code below:

<DataGrid>...                 

    <DataGridComboBoxColumn Header="Category" Width="200"                                        
                                    SelectedValueBinding="{Binding SelectedCategory, UpdateSourceTrigger=PropertyChanged}"
                                    DisplayMemberPath="CategoryName"
                                    SelectedValuePath="CategoryID">
        <DataGridComboBoxColumn.ElementStyle>
            <Style TargetType="ComboBox">
                <Setter Property="ItemsSource" Value="{Binding CategoriesList}"></Setter>
            </Style>
        </DataGridComboBoxColumn.ElementStyle>
        <DataGridComboBoxColumn.EditingElementStyle>
            <Style TargetType="ComboBox">
                <Setter Property="ItemsSource" Value="{Binding CategoriesList}"></Setter>
            </Style>                        
        </DataGridComboBoxColumn.EditingElementStyle>
   </DataGridComboBoxColumn>

The model is as follows:

public CategoryModel SelectedCategory { get; set; }
public ObservableCollection<CategoryModel> CategoriesList
{
    get;
    set;
}

Now when ever I change the selection in combobox it shows a red border, unable to commit the changes to the source.

Yael
  • 1,566
  • 3
  • 18
  • 25

1 Answers1

1

This is wrong:

<DataGridComboBoxColumn Header="Category" Width="200"                                        
                                SelectedValueBinding="{Binding SelectedCategory, UpdateSourceTrigger=PropertyChanged}"
                                DisplayMemberPath="CategoryName">

remove the SelectedValuePath or you get a type missmatch. I doubt you need the UpdateSourceTrigger either... Try to ommit.

Florian Schmidinger
  • 4,682
  • 2
  • 16
  • 28