我有一个使用具有数千行的 WPF DataGrid 的应用程序。我在使用 Vertical ScrollBar 滚动时遇到了性能问题,所以我做了一些研究,发现我可以推迟行的加载,直到通过ScrollViewer.IsDeferredScrollingEnabled = "True"
在 DataGrid 中设置完成滚动。一切正常,直到一些用户注意到滚动DataGridComboBoxColumn
DataGrid 内的选项列表现在滚动整个DataGrid
而不是ComboBox
. 仅当您在 ComboBox 滚动条上拖动鼠标时才会发生这种情况,而使用鼠标滚轮滚动时不会发生这种情况。
我使用 .net 4.0 在 vb 中整理了一个示例类,它将说明当使用鼠标拖动滚动 Column 中的 ComboBox 时出现的问题。
XAML
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="279" Width="496">
<Grid>
<DataGrid AutoGenerateColumns="False" Height="209" HorizontalAlignment="Left" Margin="12,12,0,0" Name="DataGrid1" VerticalAlignment="Top" Width="448" ItemsSource="{Binding}" ScrollViewer.IsDeferredScrollingEnabled="True">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" />
<DataGridComboBoxColumn Header="Drag Scroll through Items in a cell below" x:Name="IssueComboBoxColumn" />
</DataGrid.Columns>
</DataGrid>
</Grid>
代码
Class MainWindow
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Dim sourceList As New List(Of itemExample)
For i As Integer = 0 To 50
sourceList.Add(New itemExample("Item" & i.ToString, 4))
Next
Dim comboList As New List(Of String)
For i As Integer = 0 To 50
comboList.Add("Drop Item " & i.ToString)
Next
DataGrid1.DataContext = sourceList
IssueComboBoxColumn.ItemsSource = comboList
IssueComboBoxColumn.SelectedValuePath = "ComboItem"
End Sub
Private Class itemExample
Property Name As String
Property ComboItem As Integer
Public Sub New(name As String, comboItem As Integer)
Me.Name = name
Me.ComboItem = comboItem
End Sub
End Class
End Class