DataGrid 滚动不顺畅

发布于 2024-12-21 16:19:45 字数 4044 浏览 2 评论 0原文

我有一个 DataGrid,通常包含大约 30 行,很少超过 200 行。我的问题是,当用户使用向上/向下箭头键浏览网格内容时,它总是卡住(大约 10 到 40 秒),然后跳转一次几行

我的 DataGrid

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

它没有使用实体框架,并且数据位于非常繁忙的虚拟服务器上。

任何人都可以指出某个地方来寻找解决方案或指出我的 XAML 中的任何问题吗?

编辑

我开始认为我只是想让网格做太多事情!

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

My DataGrid

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

It's not using Entity Framework, and the data is on a very busy virtual server.

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小忆控 2024-12-28 16:19:45

您禁用 EnableRowVirtualization 和 EnableColumnVirtualization,并且还在 PreviewKeyDown="DgInvoiceLines_PreviewKeyDown" 中执行某些操作,

这可能会减慢网格滚动速度

you disable EnableRowVirtualization and EnableColumnVirtualization and also you do something in PreviewKeyDown="DgInvoiceLines_PreviewKeyDown"

this could slow down the grid scrolling

欢烬 2024-12-28 16:19:45

不起作用:

我唯一能猜到的是,当你滚动时,单元格的触发器会被触发。这可能会减慢滚动速度。尝试注释掉您的触发器,看看是否可以解决滚动问题。可能不是你想要的,但这是我能看到的唯一会减慢滚动速度的东西。

我发现 WPF 中数据网格滚动速度减慢的唯一另一件事是行内是否有图像。

方法 2:

您可以尝试的另一件事是不再使用数据网格,而只使用网格来显示数据。几乎相同,但网格的控制要轻得多。我从来没有在WPF中使用过datagrid,但我听说它的性能不是很好,所以我就使用了grid。

使用网格显示数据的一个很好的示例位于:http://msdn。 microsoft.com/en-us/library/aa480224.aspx

DID NOT WORK:

Only thing I could guess is you triggers for the cells are firing while you scroll. This could slow down the scrolling. Try commenting out your triggers and see if that fixes the scrolling issue. Might not be what you want, but that is the only thing I can see that would slow down the scrolling.

The only other thing I've seen slow down the scrolling of a data grid in WPF is if there are images inside the rows.

APPROACH 2:

Another thing you can try is switching away from using a datagrid and just use a grid to display you data. Almost the same but the grid is a lot lighter of a control. I've never used the datagrid in WPF but I've heard that it is not very good on performance, so I just used the grid.

Good example of using a grid to display you data is at: http://msdn.microsoft.com/en-us/library/aa480224.aspx

只是我以为 2024-12-28 16:19:45

您的 DataGrid 是否位于 ScrollViewer 中?我遇到一个问题,DataGrid 不会虚拟化其内容,因为 ScrollViewer 为其提供了无限的空间供其使用。
所以如果是这种情况,请将其从 ScrollViewer 中取出。

如果您不确定虚拟化是否正确,您可以下载内存分析器程序(即 ANTS)并检查性能下降时存在多少个 DataGrid 单元实例。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文