问题
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.
回答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.
来源:https://stackoverflow.com/questions/28629345/datagridcomboboxcolumn-not-updating-model-wpf