为什么 WPFToolkit DataGrid 绑定时如此慢?

发布于 2024-08-25 12:42:56 字数 803 浏览 3 评论 0原文

我有一个非常简单的测试应用程序,其中有两个对象,每个对象都有一个小的项目集合。当我选择一个对象时,我会在 WPFToolkit DataGrid 中显示其集合。

问题是存在明显的延迟,因此如果您按向上/向下键在对象之间切换选择,您会发现它无法跟上。

为什么表现这么差?

<Window x:Class="SlowGridBinding.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:Controls="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
    Title="MainWindow" Height="350" Width="525">
<StackPanel>
    <ListBox ItemsSource="{Binding Shops}" DisplayMemberPath="Name" IsSynchronizedWithCurrentItem="True"/>
    <Controls:DataGrid ItemsSource="{Binding Shops/Vegetables}" AutoGenerateColumns="True"/>
</StackPanel>

DataContext 填充了一些测试类,其中填充了 50 项随机测试数据。

I have a very simple test application where I have two objects, each with a small collection of items. when I select an object I display its collection in a WPFToolkit DataGrid.

The problem is there is a noticeable delay, such that if you press up/down keys to toggle selection between objects you can see it can't keep up.

Why is the performance so bad?

<Window x:Class="SlowGridBinding.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:Controls="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
    Title="MainWindow" Height="350" Width="525">
<StackPanel>
    <ListBox ItemsSource="{Binding Shops}" DisplayMemberPath="Name" IsSynchronizedWithCurrentItem="True"/>
    <Controls:DataGrid ItemsSource="{Binding Shops/Vegetables}" AutoGenerateColumns="True"/>
</StackPanel>

The DataContext is populated with some test classes filled with 50 items of random test data.

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

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

发布评论

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

评论(1

椵侞 2024-09-01 12:42:56

将属性 AutoGenerateColumns="True" 更改为 AutoGenerateColumns="False" 并定义数据网格的列:

<my:DataGrid AutoGenerateColumns="False" ... >
    <my:DataGrid.Columns>
        <my:DataGridTextColumn Header="Col1" Width="*" Binding="{Binding Path=Col1}" />
        <my:DataGridTextColumn Header="Col2" Width="*" Binding="{Binding Path=Col2}" />
        .
        .
        .
    </my:DataGrid.Columns>
</my:DataGrid>

这为我解决了性能问题。

Change the attribute AutoGenerateColumns="True" to AutoGenerateColumns="False" and define your columns for the datagrid:

<my:DataGrid AutoGenerateColumns="False" ... >
    <my:DataGrid.Columns>
        <my:DataGridTextColumn Header="Col1" Width="*" Binding="{Binding Path=Col1}" />
        <my:DataGridTextColumn Header="Col2" Width="*" Binding="{Binding Path=Col2}" />
        .
        .
        .
    </my:DataGrid.Columns>
</my:DataGrid>

This is what fixed the performance issues for me.

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