gpt4 book ai didi

wpf - 不合理的 WPF DataGrid 加载时间

转载 作者:行者123 更新时间:2023-12-03 20:18:55 29 4
gpt4 key购买 nike

我一直有很长的 WPF DataGrids 加载时间,我在网上找不到任何类似的报告,所以我怀疑我做错了什么。现在我确定了,因为增加布局复杂性会大大减慢执行速度。在一个非常简单的布局中,DataGrid 会立即填充,而下面的代码大约需要 3 秒才能执行。

在以下代码中,加载 150 行和 11 列需要大约 3 秒,即使每个单元格未绑定(bind)到任何属性且 AutoGenerateColumns=False。 (我有一个带有大量 RAM 的两核 2.6GHz 处理器)。

当 ItemsSource 属性在如下布局中设置时,就会出现瓶颈:

<Window x:Class="datagridtest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Border Background="LightSteelBlue" CornerRadius="10" Margin="10">
<ScrollViewer Margin="10" HorizontalScrollBarVisibility="Auto">
<Grid Margin="10,50,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />

</Grid.RowDefinitions>
<Expander IsExpanded="True" Name="expander1" Grid.Row="0">
<Grid>
<DataGrid VirtualizingStackPanel.IsVirtualizing="True" AutoGenerateColumns="false" Name="dg" Height="auto" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserSortColumns="False">
<DataGrid.Columns>
<DataGridTextColumn >
<DataGridTextColumn.Header >
<TextBlock Width="140" HorizontalAlignment="Center" TextAlignment="Center">untitled<LineBreak/>column</TextBlock>
</DataGridTextColumn.Header>
</DataGridTextColumn>



<DataGridTextColumn >
<DataGridTextColumn.Header >
<TextBlock Width="140" HorizontalAlignment="Center" TextAlignment="Center">untitled<LineBreak/>column</TextBlock>
</DataGridTextColumn.Header>
</DataGridTextColumn>

<DataGridTextColumn >
<DataGridTextColumn.Header >
<TextBlock Width="140" HorizontalAlignment="Center" TextAlignment="Center">untitled<LineBreak/>column</TextBlock>
</DataGridTextColumn.Header>
</DataGridTextColumn>

<DataGridTextColumn >
<DataGridTextColumn.Header >
<TextBlock Width="140" HorizontalAlignment="Center" TextAlignment="Center">untitled<LineBreak/>column</TextBlock>
</DataGridTextColumn.Header>
</DataGridTextColumn>

<DataGridTextColumn >
<DataGridTextColumn.Header >
<TextBlock Width="140" HorizontalAlignment="Center" TextAlignment="Center">untitled<LineBreak/>column</TextBlock>
</DataGridTextColumn.Header>
</DataGridTextColumn>


<DataGridTextColumn >
<DataGridTextColumn.Header >
<TextBlock Width="140" HorizontalAlignment="Center" TextAlignment="Center">untitled<LineBreak/>column</TextBlock>
</DataGridTextColumn.Header>
</DataGridTextColumn>

<DataGridTextColumn >
<DataGridTextColumn.Header >
<TextBlock Width="140" HorizontalAlignment="Center" TextAlignment="Center">untitled<LineBreak/>column</TextBlock>
</DataGridTextColumn.Header>
</DataGridTextColumn>

<DataGridTextColumn >
<DataGridTextColumn.Header >
<TextBlock Width="140" HorizontalAlignment="Center" TextAlignment="Center">untitled<LineBreak/>column</TextBlock>
</DataGridTextColumn.Header>
</DataGridTextColumn>

<DataGridTextColumn >
<DataGridTextColumn.Header >
<TextBlock Width="140" HorizontalAlignment="Center" TextAlignment="Center">untitled<LineBreak/>column</TextBlock>
</DataGridTextColumn.Header>
</DataGridTextColumn>

<DataGridTextColumn >
<DataGridTextColumn.Header >
<TextBlock Width="140" HorizontalAlignment="Center" TextAlignment="Center">untitled<LineBreak/>column</TextBlock>
</DataGridTextColumn.Header>
</DataGridTextColumn>

<DataGridTextColumn >
<DataGridTextColumn.Header >
<TextBlock Width="140" HorizontalAlignment="Center" TextAlignment="Center">untitled<LineBreak/>column</TextBlock>
</DataGridTextColumn.Header>
</DataGridTextColumn>



</DataGrid.Columns>
</DataGrid>
</Grid>
</Expander>

<Expander IsExpanded="true" Grid.Row="1">
<Grid>
<DataGrid AutoGenerateColumns="True" Height="auto" />
</Grid>
</Expander>

<Expander IsExpanded="true" Grid.Row="2">
<Grid>
<DataGrid AutoGenerateColumns="True" Height="auto" />
</Grid>
</Expander>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="121,-42,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click_2" />
</Grid>
</ScrollViewer>
</Border>
using System.Collections.ObjectModel;

namespace datagridtest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();


}

class row
{
public string Name { get; set; }
public double Age { get; set; }
}



private void button1_Click_2(object sender, RoutedEventArgs e)
{
ObservableCollection<row> src = new ObservableCollection<row>();

for (int i = 0; i < 150; i++)
src.Add(new row { Name = i.ToString(), Age = i / 2 });

dg.ItemsSource = src;
}
}
}

最佳答案

仅当 DataGrid 嵌入到 ScrollViewer 中时才会出现此问题,例如:

<ScrollViewer>
<Datagrid/>
</ScrollViewer>

这是有道理的,因为此配置会导致同时绘制整个 DataGrid(以便能够正确调整 ScrollViewer 的客户区的大小)。本质上,它覆盖了 DataGrid 的内置虚拟化行为,后者实现了自己的 ScrollBars,因此不必同时将其所有内容放在布局中。

换句话说,很少需要在 ScrollViewer 中嵌入 DataGrid,因为 DataGrid 有自己的自动滚动功能。

关于wpf - 不合理的 WPF DataGrid 加载时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3336921/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com