我有一个WPF DataGrid,有近8000行。这需要约。加载25秒。任何线索如何改善其性能?
此网格有3个静态ButtonType
列,其他列是动态的。我在列表中获取数据并将其分配给CodeBehind上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在加载后性能很好。完全没有滞后。我使用的是带有4 GB RAM和512 MB显卡的Core i5机器。
如果可以在此代码中进行任何优化,请与我们联系。
答案 0 :(得分:2)
DataGrid做了很多东西,但它也有很多开销。
ListView / GridView工作更多但速度更快。