2

I have a WPF application where I show and hide lots of UserControl objects. The XAML code looks something like this:

<ItemsControl ItemsSource="{Binding Path=MyListOfStuff}" >
    <Controls:MyControl Stuff="{Binding}"/>
</ItemsControl>

Creating those objects takes a lot of time. Profiling shows that 'InitializeComponent' takes a significant amount of time for the MyControl objects. I want to pre create these objects to reduce that time. How can I do that? I still need the xaml code and don't want to replace all of it with just code.

It's not possible to use virtualization because the items in the list are sometimes bigger than the view, so CanContentScroll must be False.

Markus Johansson
  • 3,733
  • 8
  • 36
  • 55
  • 3
    Have a look at this: http://stackoverflow.com/questions/2783845/wpf-virtualizing-an-itemscontrol Although I'm not sure if virualizing would help in your case since you're showing and hiding not scrolling. – Ray Jan 10 '12 at 14:12
  • Have a look at http://stackoverflow.com/questions/22218506/wpf-canvas-based-itemscontrol-with-minimum-recycled-items/22220980#comment33743843_22220980 for a CachingItemsControl implementation – Omer Raviv Mar 07 '14 at 10:21

2 Answers2

1

There are two aspects to solve your problem. even you can use both of them..

  1. UI Virtualization
  2. Data Virtualization

You should have a look at

http://blogs.microsoft.co.il/blogs/tomershamam/archive/2009/10/01/ui-virtualization-vs-data-virtualization-part-2.aspx

also if you are using .net 4.0 you can use Lazy class to support Data Virtualization easily otherwise you will have to create some classes to support that.

Regards.

Shoaib Shaikh
  • 4,565
  • 1
  • 27
  • 35
1

It is possible to do UI caching in WPF, but I'm quite sure it is not what you are looking for.

Bitmap caching is useful when one visual is painted more than once, so it is rendered only once and for subsequent renderings only copied from the cache.

You will have to have a look at virtualization (see Shoaib's answer).

Article about bitmap caching.

Matěj Zábský
  • 16,909
  • 15
  • 69
  • 114