WPFToolkit DataGrid: Combobox column does not update selectedvaluebinding immediately

眉间皱痕 提交于 2019-12-19 07:49:11

问题


I'm using WPF Toolkit DataGrid and DataGridComboBoxColumn. Everything works well, except that when selection change happens on the combobox, the selectedvaluebinding source is not updated immediately. This happens only when the combobox loses focus. Has anyone run into this issue and any suggestions solutions ?

Here's the xaml for the column:

<toolkit:DataGridComboBoxColumn Header="Column" SelectedValueBinding="{Binding Path=Params.ColumnName, UpdateSourceTrigger=PropertyChanged}"
                DisplayMemberPath="cName"
                SelectedValuePath="cName">
                <toolkit:DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding Info.Columns}" />
                    </Style>
                </toolkit:DataGridComboBoxColumn.ElementStyle>
                <toolkit:DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding Info.Columns}" />
                    </Style>
                </toolkit:DataGridComboBoxColumn.EditingElementStyle>
            </toolkit:DataGridComboBoxColumn>

回答1:


The problem is that the cell remains in Edit mode until you leave the cell and the changes are committed

Solution: you need to create your own column type to override the default behavior

code:

public class AutoCommitComboBoxColumn : Microsoft.Windows.Controls.DataGridComboBoxColumn
{
    protected override FrameworkElement GenerateEditingElement(Microsoft.Windows.Controls.DataGridCell cell, object dataItem)
    {
        var comboBox = (ComboBox)base.GenerateEditingElement(cell, dataItem);
        comboBox.SelectionChanged += ComboBox_SelectionChanged;
        return comboBox;
    }

    public void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        CommitCellEdit((FrameworkElement)sender);
    }
}



回答2:


UpdateSourceTrigger=PropertyChanged option is crucial here, it doesn't do without it.



来源:https://stackoverflow.com/questions/2869978/wpftoolkit-datagrid-combobox-column-does-not-update-selectedvaluebinding-immedi

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