Wpf DataGrid ComboxItems行为怪异
我有一个带有以下模板的DataGridWpf DataGrid ComboxItems行为怪异,wpf,combobox,wpfdatagrid,Wpf,Combobox,Wpfdatagrid,我有一个带有以下模板的DataGrid <DataGridTemplateColumn Header="Priority" Width="60" > <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox x:Name="comboPriority" SelectionChanged="DataGridComboBoxSelectionC
<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>
我可以选择一个选项,将其保存到DB,并在初始加载时正确查看所选值。但是,当我滚动时,所选值在其他行上多次出现/消失
举个例子。如果我从第一行的组合框中选择一个值,我会在整个DataGrid中多次看到该值
我尝试在下拉选择后立即刷新网格,但DataGrid的行为相同。这是因为UI虚拟化。您可以通过将VirtualzingPanel.IsVirtualization attached属性设置为false来禁用它: 请注意,这可能会对滚动性能产生负面影响
解决此问题的最佳方法是将ComboBox的SelectedItem属性绑定到填充DataGrid的数据对象类型的属性。comboPriority\u加载的事件处理程序做什么?我使用它从DB设置ComboBox的选定值。这就是问题的原因。不要那样做。似乎您误解了WPF datagrid的工作原理。
<DataGrid ... VirtualizingPanel.IsVirtualizing="False">