Optimizing Performance in WPF

Performance is the biggest challenge that every developer encounters during development. There can be many reasons sometimes that they don't get sufficient time to complete it, such as a lack of technical knowledge, lack of domain knowledge, impacts not analyzed and many more reasons. Today everyone wants an application that is fast and responsive. So if we focus particularly on performance in WPF applications then we used to see developers are facing many issues related to improving performance in WPF. Because they always used to try to improve LINQ queries, code behind yet they never focus on the XAML part. So, this article we will try to focus on some WPF performance improvements.

Binding

The most important performance cause in WPF is binding. Try to always optimize a number of bindings, triggers, control templates and styles you are using. For example, if I need to show one DataGrid that contains firstname and lastname separated by a space and I am creating two columns in the DataGrid and binding it. Then instead of creating two columns and binding it twice we can directly combine in the code behind and bind that within a column of the DataGrid.

So in the following code we have decreased the number of bindings from 2 to 1.

CS file code: completename=firstname +” ”+lastname;

  1. <DataGrid ItemsSource="{Binding ListName}">  
  2.   
  3.      <DataGridTextColumn Binding="{Binding firstname}">                 
  4.   
  5.      </DataGridTextColumn>  
  6.   
  7.      <DataGridTextColumn Binding="{Binding lastname}">  
  8.   
  9.      </DataGridTextColumn>  
  10.   
  11.  </DataGrid>  
  12.   
  13.   
  14.   
  15.  <DataGrid ItemsSource="{Binding ListName}">  
  16.   
  17.      <DataGridTextColumn Binding="{Binding completename}">  
  18.   
  19.      </DataGridTextColumn>             
  20.   
  21.  </DataGrid>  

Converter

A Converter also causes a performance issue if you are using it when it needs to do many calculations because when your template is loading then for each item in that template it will call that converter and if you write a lot of code in the converter then it will cause a performance issue. So, always try to improve the converter or write one or two lines of code in your converter.

Triggers

If you are developing a WPF application then triggers play an important role. But always try to improve the trigger otherwise it can cause a performance issue because triggers also use bindings and bindings are heavier.

Example: In the first code you can see that I have used four multi-data triggers containing 8 bindings and in the second code I have used fewer triggers containing only four bindings. So if you are improving binding then you improve performance.

Code 1

trigger

Code 2

trigger style

Virtualization

Try to use virtualization in a grid or list box. It makes the application faster because only the visible data is loaded instead of the entire data. You can use a VirtualizingStackPanel instead of a stack panel.

Virtualization Mode and Scrolling Unit: Use VirtualizingPanel.VirtualizationMode="Recycling" and VirtualizingPanel.ScrollUnit="Pixel" for faster scrolling.

Progress Indicator

Whenever your application is loading or a time consuming part is happening then always try to implement a background worker and show the progress to the user so that the application will be responsive. You can use the exceed WPF toolkit for a progress indicator.

Use the Most Efficient Panel

The complexity of the layout process is directly based on the layout behavior of the Panel-derived elements you use. For example, a Grid or StackPanel control provides much more functionality than a Canvas control. The price for this greater increase in functionality is a greater increase in performance costs. However, if you do not require the functionality that a Grid control provides, you should use the less costly alternatives, such as a Canvas.

I hope this will help in improving performance in WPF.