Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPFToolkit DataGrid: Combobox column does not update selectedvaluebinding immediately

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>
like image 818
neblinc1 Avatar asked May 19 '10 22:05

neblinc1


2 Answers

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

like image 123
mr_esp Avatar answered Sep 23 '22 20:09

mr_esp


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);
    }
}
like image 23
Enrique G Avatar answered Sep 21 '22 20:09

Enrique G