Piero
Piero

Reputation: 31

WPF Datagrid RowVirtualization problem with framework .NET 4.8

With the new .NET 4.8 framework, I detected problems in the WPF datagrid with comboboxes when scrolling up and down:

Problem with the below image: "The image shows a few empty combo boxes. When the program was loaded, these combo boxes were filled with data but as you scroll up and down in the application, the data disappears"

enter image description here

If I set EnableRowVirtualization to false in the datagrid, the problem can not be detected.

With previous versions of the framework (4.7 or older) I didn't have this problem, either with EnableRowVirtualization configured on true or false.

Why?

<DataGrid Focusable="True" Grid.Column="0" Grid.Row="0" HeadersVisibility="Column" RowHeaderWidth="0" AutoGenerateColumns="False" ItemsSource="{Binding People}" CanUserAddRows="False" CanUserDeleteRows="False" SelectionUnit="FullRow" IsTabStop="True">
  <DataGrid.Columns>
    <DataGridTextColumn Header="Name" IsReadOnly="True" MinWidth="50" Binding="{Binding Name, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"/>
    <DataGridTemplateColumn Header="Age Old">
      <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <ComboBox ItemsSource="{Binding Ages, TargetNullValue=''}" SelectedValue="{Binding Age, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=LostFocus, TargetNullValue=''}">
                            </ComboBox>
        </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
  </DataGrid.Columns>
</DataGrid>

Upvotes: 3

Views: 1102

Answers (1)

Sam Bent - MSFT
Sam Bent - MSFT

Reputation: 276

This is a WPF bug in .NET 4.8 - it's fixed in an update that's in the pipeline for release this fall. Here's the KB blurb:

A WPF ComboBox (or any Selector) within a DataGrid cell can attempt to change its selection properties (SelectedIndex, SelectedItem, SelectedValue) when the cell's data item is re-virtualized or removed from the underlying collection. This can happen if the Selector's ItemSource property is data-bound via the cell's DataContext.
Depending on the virtualization mode and the bindings declared for the selection properties, the symptoms can include unexpected changes (to null) of the data item's properties, or unexpected display (as null) of other data items that happen to re-use the UI formerly attached to the re-virtualized item.

In the meantime, you can work around the bug by binding ComboBox.ItemsSource in some way that doesn't depend on the DataContext. Often the collection doesn't really depend on the individual data item, so you can bind to a fixed collection. In your example, if the Ages property points to the same collection for each data item, you can bind directly to it: ItemsSource="{Binding Source={StaticResource Ages}}" in the scope of a resource declared as <CollectionViewSource x:Key="Ages"/> whose Source points to the (shared) list of permissible ages. (There are many ways to set Source: programmatically in initialization code, bind it to a property of a higher-level view model, etc.)

Upvotes: 2

Related Questions