Slow loading WPF Form.

Total Post:149

Points:1043
WPF 
WPF
 2091  View(s)
Ratings:
Rate this:
Hi Guys

I've been search for a solution to a slow loading wpf form. It seems that virtualization should help me out, but I must be missing something.

I'm loading 35k orders and it takes 15 seconds to load. My guess is that virtualization isn't actually working for me.

<UserControl x:Class="IntTranWin.Modules.Orders.OrderSummary.OrdersSummaryView"
         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" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300">
  <ListView BorderThickness="0" Width="Auto" ItemsSource="{Binding Path=Orders.Items}" AlternationCount="2"
            IsSynchronizedWithCurrentItem="True" VirtualizingStackPanel.IsVirtualizing="True"
            VirtualizingStackPanel.VirtualizationMode="Recycling" ScrollViewer.IsDeferredScrollingEnabled="True">
    <ListView.ItemsPanel>
      <ItemsPanelTemplate>
        <VirtualizingStackPanel />
      </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    <ListView.View>
      <GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Orders">
        <GridViewColumn Header="Number" DisplayMemberBinding="{Binding Path=OrderNumber}" Width="65" />
      </GridView>
    </ListView.View>
  </ListView>
</UserControl>

Thanks 

  1. Post:105

    Points:735
    Re: Slow loading WPF Form.

    Hi!

    There wasn't really anything wrong with wpf I provided. The problem was in a styling of ScrollViewer for the GridView which disabled virtualization. 

    <Style x:Key="{x:Static
    GridView.GridViewScrollViewerStyleKey}" TargetType="{x:Type
    ScrollViewer}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type ScrollViewer}">
          <Grid Background="{TemplateBinding Background}">
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="*" />
              <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
              <RowDefinition Height="*" />
              <RowDefinition Height="Auto" />
            </Grid.RowDefinitions> 
            <DockPanel Margin="{TemplateBinding Padding}">
              <ScrollViewer DockPanel.Dock="Top" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"
                            Focusable="false">
                <Border CornerRadius="4" Background="{StaticResource DefaultedBorderBrush}" Padding="4" Margin="0,4,0,4">
                  <GridViewHeaderRowPresenter Margin="2,0,2,0"
                                              Columns="{Binding Path=TemplatedParent.View.Columns,RelativeSource={RelativeSource TemplatedParent}}"
                                              ColumnHeaderContainerStyle="{Binding Path=TemplatedParent.View.ColumnHeaderContainerStyle,RelativeSource={RelativeSource TemplatedParent}}"
                                              ColumnHeaderTemplate="{Binding Path=TemplatedParent.View.ColumnHeaderTemplate,RelativeSource={RelativeSource TemplatedParent}}"
                                              ColumnHeaderTemplateSelector="{Binding Path=TemplatedParent.View.ColumnHeaderTemplateSelector,RelativeSource={RelativeSource TemplatedParent}}"
                                              AllowsColumnReorder="{Binding Path=TemplatedParent.View.AllowsColumnReorder,RelativeSource={RelativeSource TemplatedParent}}"
                                              ColumnHeaderContextMenu="{Binding Path=TemplatedParent.View.ColumnHeaderContextMenu,RelativeSource={RelativeSource TemplatedParent}}"
                                              ColumnHeaderToolTip="{Binding Path=TemplatedParent.View.ColumnHeaderToolTip,RelativeSource={RelativeSource TemplatedParent}}"
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                </Border> 
              </ScrollViewer> 
              <ScrollContentPresenter Name="PART_ScrollContentPresenter" KeyboardNavigation.DirectionalNavigation="Local" />
            </DockPanel> 
            <ScrollBar Name="PART_HorizontalScrollBar" Orientation="Horizontal" Grid.Row="1"
                       Maximum="{TemplateBinding ScrollableWidth}" ViewportSize="{TemplateBinding ViewportWidth}"
                       Value="{TemplateBinding HorizontalOffset}" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" /> 
            <ScrollBar Name="PART_VerticalScrollBar" Grid.Column="1" Maximum="{TemplateBinding ScrollableHeight}"
                       ViewportSize="{TemplateBinding ViewportHeight}" Value="{TemplateBinding VerticalOffset}"
                       Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" /> 
          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter> 

    I hope it will work fine.

      Modified On Mar-30-2018 11:14:34 PM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!