代码之家  ›  专栏  ›  技术社区  ›  nPcomp

DataGrid Comboboxitems的行为异常

  •  0
  • nPcomp  · 技术社区  · 8 年前

    我有一个 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>
    

    我可以选择一个选项,将其保存到数据库中,并在初始加载时正确地看到所选的值。但是,当我滚动时,所选值在其他行上多次出现/消失。

    作为一个例子。如果我从第一行的 ComboBox ,我在整个 数据网格 .

    我试图在下拉选择之后刷新网格,但是 数据网格 表现也一样。

    1 回复  |  直到 8 年前
        1
  •  1
  •   mm8    8 年前

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

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

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

    解决这个问题的最好方法是 SelectedItem 财产 ComboBox 填充的数据对象类型的属性 DataGrid 用。