DataGrid滚动不流畅 [英] DataGrid not scrolling smoothly
问题描述
我有一个DataGrid,通常包含大约30行,很少会超过200行。我的问题是,当用户使用向上/向下箭头键浏览网格的内容时,它一直卡住(大约10到40秒)然后一次跳转几行
I have a DataGrid which usually contains approx 30 rows it rarely goes above 200. My problem is when the user goes through the contents of the grid with the up/down arrow keys it keeps getting stuck(approx 10 to 40 seconds) and then jumping several rows at a time
我的数据网格
<DataGrid Name="DgInvoiceLines" KeyUp="DgInvoiceLines_KeyUp" BorderBrush="Black"
RowBackground="Silver" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible"
AutoGenerateColumns="False" SelectionUnit="FullRow" SelectionMode="Extended" CanUserAddRows="True" CanUserDeleteRows="True"
HorizontalContentAlignment="Center" CellEditEnding="DgInvoiceLines_CellEditEnding" VerticalContentAlignment="Center"
PreviewKeyDown="DgInvoiceLines_PreviewKeyDown" SelectionChanged="DgInvoiceLines_SelectionChanged"
CurrentCellChanged="DgInvoiceLines_CurrentCellChanged" ItemsSource="{Binding}" BorderThickness="0,2,0,0"
EnableColumnVirtualization="False" EnableRowVirtualization="False" ScrollViewer.CanContentScroll="False">
<DataGrid.Columns>
<DataGridTextColumn Width="Auto" Header="ProductCode" Binding="{Binding ProductCode}"/>
<DataGridTextColumn Width="250" Header="Description" Binding="{Binding Description}" FontSize="14"/>
<DataGridTextColumn Width="61" Header="Inv_Quantity" Binding="{Binding Inv_Quantity}"/>
<DataGridTextColumn Width="63" Header="Grn_Quantity" Binding="{Binding Grn_Quantity}"/>
<DataGridTextColumn Width="59" Header="Inv_Price" Binding="{Binding Inv_Price}"/>
<DataGridTextColumn Width="61" Header="Ord_Price" Binding="{Binding Grn_Price}"/>
<DataGridTextColumn Width="72" Header="Inv_Total" Binding="{Binding Inv_Total}"/>
<DataGridTextColumn Width="74" Header="Grn_Total" Binding="{Binding Grn_Total}"/>
<DataGridTextColumn Width="58" Header="AnalCode" Binding="{Binding AnalCode}"/>
<DataGridTextColumn Width="60" Header="Vat_Rate" Binding="{Binding Vat_Rate}"/>
<DataGridTextColumn Width="60" Header="Vat" Binding="{Binding Vat}" IsReadOnly="True"/>
<DataGridTextColumn Width="Auto" Header="GrnNo" Binding="{Binding GrnNo}"/>
<DataGridCheckBoxColumn Width="Auto" Binding="{Binding Line_Correct}" Header="" >
<DataGridCheckBoxColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"/>
</Style>
</DataGridCheckBoxColumn.CellStyle>
</DataGridCheckBoxColumn>
<DataGridTextColumn Width="Auto" Header="Comment" Binding="{Binding Comment}"/>
<DataGridTextColumn Width="Auto" Header="PerP" Binding="{Binding OuterUnits}" IsReadOnly="True"/>
</DataGrid.Columns>
<DataGrid.CellStyle >
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsKeyboardFocused" Value="True" >
<Setter Property="Background" Value="White" />
<Setter Property="Foreground" Value="Black" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightSteelBlue"/>
</DataGrid.Resources>
</DataGrid>
它没有使用Entity Framework,并且数据位于非常繁忙的虚拟服务器上。
It's not using Entity Framework, and the data is on a very busy virtual server.
任何人都可以指向某个地方寻找解决方案或指出我的XAML中的任何问题吗?
Can anyone point to somewhere to look for a solution or indicate any problems in my XAML?
EDIT
我开始认为我只是想让网格做得太多!
I'm starting to think that I'm just trying to get the grid to do too much!
推荐答案
您的DataGrid是否在 ScrollViewer 中?我遇到一个问题,即DataGrid无法对其内容进行虚拟化,因为ScrollViewer为它提供了无限的空间,然后可以使用它。
因此,在这种情况下,请将其从ScrollViewer中取出。
Is your DataGrid in a ScrollViewer? I had an issue where a DataGrid wouldn't virtualize its content because the ScrollViewer provided it with infinite space which it then used. So if this is the case, take it out of the ScrollViewer.
如果不确定虚拟化是否正确,可以下载内存分析器程序(即ANTS),并在性能下降时检查有多少个DataGrid单元实例。
If you're not sure whether it virtualizes properly, you can download a memory profiler program (i.e. ANTS) and check how many instances of DataGrid cells are in existence at the time the performance goes down.
这篇关于DataGrid滚动不流畅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!