DataGridComboBoxColumn not updating model WPF

孤街醉人 提交于 2019-12-23 05:33:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!