DataGrid comboBox Items 行为怪异

DataGrid comboBoxItems acting weird

我有一个 DataGrid 具有以下模板

<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>

我可以 select 一个选项,将其保存到数据库并在初始加载时正确查看 selected 值。但是,当我滚动时,selected 值在其他行上多次 appear/disappear。

举个例子。如果我 select 第一行的 ComboBox 中的一个值,我会在 DataGrid.

中多次看到该值

我试图在下拉 selection 后立即刷新网格,但 DataGrid 表现相同。

这是因为 UI 虚拟化。您可以通过将附加的 VirtualizingPanel.IsVirtualizing 属性 设置为 false:

来禁用它
<DataGrid ... VirtualizingPanel.IsVirtualizing="False">

请注意,这可能会对滚动性能产生负面影响。

解决此问题的最佳方法是将 ComboBoxSelectedItem 属性 绑定到您填充的数据对象类型的 属性 DataGrid