nPcomp
nPcomp

Reputation: 9843

DataGrid comboBoxItems acting weird

I have a DataGrid with the following template

<DataGridTemplateColumn Header="Priority" Width="60" >
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox x:Name="comboPriority" SelectionChanged="DataGridComboBoxSelectionChanged" FontWeight="Bold" Loaded="comboPriority_Loaded">
                <ComboBoxItem Background="Red">
                    <Label Background="Red" HorizontalContentAlignment="Center" Width="40">A</Label>
                </ComboBoxItem>
                <ComboBoxItem Background="#FFFFC000">
                    <Label Background="#FFFFC000" HorizontalContentAlignment="Center" Width="40">B</Label>
                </ComboBoxItem>
                <ComboBoxItem Background="Yellow">
                    <Label Background="Yellow" HorizontalContentAlignment="Center" Width="40">C</Label>
                </ComboBoxItem>
                <ComboBoxItem Background="#FF92D050">
                    <Label Background="#FF92D050" HorizontalContentAlignment="Center" Width="40">D</Label>
                </ComboBoxItem>
                <ComboBoxItem Background="#FF00B0F0">
                    <Label Background="#FF00B0F0" HorizontalContentAlignment="Center" Width="40">E</Label>
                </ComboBoxItem>
                <ComboBoxItem Background="#FFB1A0C7">
                    <Label Background="#FFB1A0C7" HorizontalContentAlignment="Center" Width="40">F </Label>
                </ComboBoxItem>
                <ComboBoxItem Background="#FFFF3399">
                    <Label Background="#FFFF3399" HorizontalContentAlignment="Center" Width="40">G</Label>
                </ComboBoxItem>
            </ComboBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

I can select an option, save it to the DB and see the selected values on initial load correctly. However, while I am scrolling, the selected values appear/disappear multiple times on other rows.

As an example. if I select a value from the first row's ComboBox, I see that value many times throughout the DataGrid.

I tried to refresh the grid right after drop-down selection but the DataGrid behaved the same.

Upvotes: 0

Views: 41

Answers (1)

mm8
mm8

Reputation: 169200

This is because of the UI virtualization. You can disable it by setting the VirtualizingPanel.IsVirtualizing attached property to false:

<DataGrid ... VirtualizingPanel.IsVirtualizing="False">

Beware that this may affect the scrolling performance negatively.

The best way to solve this would be to bind the SelectedItem property of the ComboBox to a property of the type of the data objects that you populate the DataGrid with.

Upvotes: 1

Related Questions