我有一个拥有近8000行的WPF DataGrid。它需要大约25秒才能加载完毕。有什么方法可以提高它的性能吗?
这个表格有3个静态的ButtonType
列,其他列都是动态的。我在代码后台将数据获取到的列表分配给DataGrid的ItemsSource
属性。
以下是XAML代码:
<UserControl x:Class="TransportApp.Views.CommonScreen.SideBar.DetailsGrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid Margin="5,0,0,0" VirtualizingStackPanel.IsVirtualizing="True">
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="5" />
</Grid.RowDefinitions>
<Grid Grid.Row="0" HorizontalAlignment="Left">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="30"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Label Name="DisplayHeader" Grid.Row="0" Content="Information" VerticalAlignment="Center" FontSize="20" />
<DatePicker Name="dtGridFilterDate" Grid.Column="1" Grid.Row="0" Margin="4" SelectedDate="{x:Static sys:DateTime.Now}"/>
<mui:ModernButton Name="Cmd_Refresh" Grid.Column="2" Grid.Row="0" ToolTip="Click to Refresh View" IconData="{StaticResource RefreshIconData}" EllipseStrokeThickness="2" EllipseDiameter="26" IconHeight="15" IconWidth="15" Click="Cmd_Refresh_Click"/>
<DockPanel Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1">
<Label FontWeight="Bold" DockPanel.Dock="Left" Name="lblFilter" Content="Filter" VerticalAlignment="Center"/>
<TextBox DockPanel.Dock="Right" Margin="4" HorizontalAlignment="Stretch" Grid.Column="1" VerticalContentAlignment="Center" Name="filterBox1"/>
</DockPanel>
<mui:ModernButton Name="cmdSearch" Grid.Column="2" Grid.Row="1" ToolTip="Click to Search" IconData="{StaticResource SearchData}" EllipseStrokeThickness="2" EllipseDiameter="26" IconHeight="11" IconWidth="11" Click="cmdSearch_Click"/>
</Grid>
<DataGrid Name="DG_Details" Grid.Row="1"
AutoGenerateColumns="True" IsReadOnly="True"
CanUserAddRows="False" CanUserDeleteRows="False"
SelectionMode="Single" EnableRowVirtualization="True"
VirtualizingStackPanel.IsVirtualizing="True"
AutoGeneratingColumn="DG_Details_AutoGeneratingColumn"
HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Disabled">
<DataGrid.Columns>
<DataGridTemplateColumn x:Name="dgEditColumn">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<mui:ModernButton Name="Cmd_Edit" Click="Cmd_Edit_Click" ToolTip="Edit" IconData="{StaticResource EditIconData}" EllipseStrokeThickness="2" EllipseDiameter="20" IconHeight="10" IconWidth="10"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn x:Name="dgDeleteColumn">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<mui:ModernButton Name="Cmd_Delete" Click="Cmd_Delete_Click" ToolTip="Delete" IconData="{StaticResource DeleteIconData}" EllipseStrokeThickness="2" EllipseDiameter="20" IconHeight="10" IconWidth="10"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn x:Name="dgPrintColumn" Visibility="Hidden">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<mui:ModernButton Name="Cmd_Print" Click="Cmd_Print_Click" ToolTip="Re-Print document" IconData="{StaticResource PrintIconData}" EllipseStrokeThickness="2" EllipseDiameter="20" IconHeight="10" IconWidth="10"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl>
数据加载逻辑
var customerList = (from c in context.Customers
where c.IsDeleted == false
select new
{
FirstName = c.FirstName,
LastName = c.LastName,
Phone = c.Phone,
CustomerId = c.CustomerId
}).ToList();
DG_Details.ItemsSource = customerList;
DG_Details.Items.Refresh();
这里需要注意的是,DataGrid在加载后的性能非常好,没有任何卡顿。我正在使用一台配备Core i5处理器、4 GB内存和512 MB显卡的机器。
如果有优化代码的方法,请告诉我。