Performance when loading list elements

Heiko 1,261 Reputation points
2024-10-06T15:08:00.5766667+00:00

I have about one hundred entries in a list. These are of data type DataA. A single entry within this list is of the data type DataB. DataB is derived from DataA. During debugging, I see in the output window that for all list elements of the data type DataA, all properties for the data type DataB are queried, although the grid, whose elements access the properties of DataB, is hidden. Although I can understand that it is necessary to query these properties and update the GUI elements with the values from DataB, I do not need to update the GUI elements if the grid surrounding them is hidden.

I used the StopWatch to measure the time it takes from the output of the constructor of the MainWindow to the call of Window_Loaded() and found that with the additional grid (for Player Controls) 30 to 40 percent more time is needed.

Is there a way to improve the performance?

public class DataA

{

// ...

public virtual Visibility => Visibility.Collapsed;

}

public class DataB : DataA // only for Player Controls

{

// ...

public override Visibility => Visibility.Visible;

}

<ItemsControl>

<ItemsControl.ItemTemplate>

<DataTemplate>

<Grid>

<TextBlock Foreground="{Binding ...}" Text="{Binding ...}" />

<TextBlock Foreground="{Binding ...}" Text="{Binding ...}" />

<TextBlock Foreground="{Binding ...}" Text="{Binding ...}" />

<TextBlock Foreground="{Binding ...}" Text="{Binding ...}" />

<!--...-->

<!--Player Controls-->

<Grid Visibility="{Binding PlayerVisibility}">

<TextBlock Text="{Binding ...}" />

<TextBlock Text="{Binding ...}" />

<TextBlock Text="{Binding ...}" />

<TextBlock Text="{Binding ...}" />

<Grid>

<Border BorderBrush="{Binding ...}">

<StackPanel>

<TextBlock Foreground="{Binding ...}" Text="{Binding ...}" />

<TextBlock Foreground="{Binding ...}" Text="{Binding ...}" />

<TextBlock Foreground="{Binding ...}" Text="{Binding ...}" />

</StackPanel>

</Border>

<!--...-->

</Grid>

</Grid>

</Grid>

</DataTemplate>

</ItemsControl.ItemTemplate>

</ItemsControl>

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,784 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,011 questions
{count} votes

Accepted answer
  1. Hongrui Yu-MSFT 2,465 Reputation points Microsoft Vendor
    2024-10-07T07:36:15.4133333+00:00

    Hi,@Heiko. Welcome to Microsoft Q&A. 

    You could optimize the performance of loading List elements from the following perspectives.

     

    1.Control virtualization. You could refer to the following document to virtualize your controls:

    https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/optimizing-performance-controls?view=netframeworkdesktop-4.8.

    2.Set not only Visibility but also IsEnabled for Player Controls.

     

    3.Page loading. Try to reduce the amount of data loaded at one time to improve performance.

     

    4.Simplify styles and templates. Complex styles and templates will increase rendering time, try to simplify them.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.