Search code examples
.netwpfcomboboxdatagridtemplatecolumn.net-4.7.2

WPF: strange ComboBox behavior after update to .net framework 4.7.2


After upgrade to .NET Framework 4.7.2 I noticed a strange behavior of the ComboBox when it is into a DataGridTemplateColumn.CellEditingTemplate.

<DataGrid
        IsReadOnly="False"
        ItemsSource="{Binding Path=Items, Mode=OneWay}">
        <DataGrid.Columns>
            <DataGridTemplateColumn
                Header="Test"
                IsReadOnly="False"
                Width="70">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock
                            VerticalAlignment="Center"
                            TextAlignment="Left"
                            Text="{Binding Path=Id, Mode=OneWay}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox
                            Name="cmbTest"
                            DisplayMemberPath=""
                            HorizontalAlignment="Stretch"
                            IsEditable="True"
                            IsEnabled="True"
                            IsReadOnly="False"
                            IsSynchronizedWithCurrentItem="False"
                            ItemsSource="{Binding Path=Items2, Mode=OneWay}"
                            Margin="0"
                            SelectedItem="{x:Null}"
                            SelectedValue="{x:Null}"
                            SelectedValuePath=""
                            Text=""
                            VerticalAlignment="Center"
                            Visibility="Visible"
                            SelectionChanged="cmbTest_SelectionChanged">
                        </ComboBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

Before upgrade, event "cmbTest_SelectionChanged" was raised only when I change the SelectedItem, but now (after 4.7.2. upgrade) "cmbTest_SelectionChanged" is raised (with "null" SelectedItem!) also when I exit by cell editing...and this is a problem for my application.

How can I avoid this issue?

UPDATE 16/06/2018: I have discovered that the issue occurs only when the bind list to the ComboBox (Items2) is a property of the SelectedItem. If I bind the list to external datacontext (FindAncestor...) it seems works properly.


Solution

  • This is a consequence of a bugfix documented here: Fixed data corruption arising when a scrolling DataGrid with VirtualizationMode=Recycling. [405066, PresentationFramework.dll, Bug].

    The corruption arose because the bindings in a cell's editing template continued to react to changes, even after the cell exited editing. The fix disconnected all bindings when exiting editing. In your case that sets ComboBox.ItemsSource to null, which raises a SelectionChanged event. You can easily ignore this event by testing whether ComboBox.ItemsSource is null.