WPF DataGrid为什么性能很慢

时间:2016-10-27 13:21:11

标签: c# wpf winforms mvvm datagrid

我有一个包含70-90行和9-15列的表。在WPF中加载这个需要400毫秒(没有绑定错误),在WinForms中可能需要10毫秒。为什么这么大的性能差异? (.NET 4.5)。

<Grid Grid.Row="1" HorizontalAlignment="Stretch"
                   VerticalAlignment="Stretch"
                   Initialized="Grid_Initialized"
                   Loaded="Grid_Loaded">
  <DataGrid ItemsSource="{Binding Gu45Documents}" 
            EnableColumnVirtualization="True" 
            AutoGenerateColumns="False" 
            EnableRowVirtualization="True" 
            VirtualizingPanel.IsVirtualizingWhenGrouping="True"
            IsReadOnly="True" 
            SelectedItem="{Binding SelectedItem}">
    <DataGrid.Columns>
      <DataGridTextColumn Header="State"
                          Binding="{Binding DocState.State}"
                          Width="100"/>
      <DataGridTextColumn Header="StateDate"
                          Binding="{Binding DocState.StateDate}"
                          Width="100"/>
      <DataGridTextColumn Header="Esr_mount_name"
                          Binding="{Binding EGu45.Gu45.Esr_mount_name}"
                          Width="100"/>
      <DataGridTextColumn Header="Name"
                          Binding="{Binding EGu45.Gu45.Branch.Name}"
                          Width="100"/>
      <DataGridTextColumn Header="Delivery_time"
                          Binding="{Binding EGu45.Gu45.Delivery_time}"
                          Width="100"/>
      <DataGridTextColumn Header="Name"
                          Binding="{Binding EGu45.Gu45.Direction.Name}"
                          Width="100"/>
      <DataGridTextColumn Header="Esr_mount_name"
                          Binding="{Binding EGu45.Gu45.Esr_mount_name}"
                          Width="100"/>
      <DataGridTextColumn Header="Taking_time"
                          Binding="{Binding EGu45.Gu45.Taking_time}"
                          Width="100"/>
      <DataGridTextColumn Header="Delivery_time"
                          Binding="{Binding EGu45.Gu45.Delivery_time}"
                          Width="100"/>
    </DataGrid.Columns>
  </DataGrid>
</Grid>

在WPF中使用WinForm控件可能更好吗?但它会打破所有MVVM模型。

<WindowsFormsHost>
  <wf:DataGrid x:Name="dataGrid"/>
</WindowsFormsHost>

也许在WPF中存在DataGrid的自定义快速实现?

所有页面看起来像

<UserControl x:Class="WPFGO.Gu45Register.View.Gu45ListView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WPFGO.ShippingDocumentsRegister.CustomControls"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
             xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
             mc:Ignorable="d" 
             DataContext="{Binding Source={StaticResource Locator}, Path=Gu45ListViewModel}"
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Initialized="Grid_Initialized" Loaded="Grid_Loaded">
        <DataGrid ItemsSource="{Binding Gu45Documents}" 
                      EnableColumnVirtualization="True" 
                      AutoGenerateColumns="False" 
                      EnableRowVirtualization="True" 
                      VirtualizingPanel.IsVirtualizingWhenGrouping="True"
                      IsReadOnly="True" 
                      SelectedItem="{Binding SelectedItem}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="State" Binding="{Binding DocState.State}" Width="100"/>
                <DataGridTextColumn Header="StateDate" Binding="{Binding DocState.StateDate}" Width="100"/>
                <DataGridTextColumn Header="Esr_mount_name" Binding="{Binding EGu45.Gu45.Esr_mount_name}" Width="100"/>
                <DataGridTextColumn Header="Name" Binding="{Binding EGu45.Gu45.Branch.Name}" Width="100"/>
                <DataGridTextColumn Header="Delivery_time" Binding="{Binding EGu45.Gu45.Delivery_time}" Width="100"/>
                <DataGridTextColumn Header="Name" Binding="{Binding EGu45.Gu45.Direction.Name}" Width="100"/>
                <DataGridTextColumn Header="Esr_mount_name" Binding="{Binding EGu45.Gu45.Esr_mount_name}" Width="100"/>
                <DataGridTextColumn Header="Taking_time" Binding="{Binding EGu45.Gu45.Taking_time}" Width="100"/>
                <DataGridTextColumn Header="Delivery_time" Binding="{Binding EGu45.Gu45.Delivery_time}" Width="100"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</UserControl>

0 个答案:

没有答案