0

I am working with DataTemplate to dynamically change the view of my UI.

However, I found that the performance is quite unacceptable.

Here is my code:

Xaml (Views.xaml):

<ResourceDictionary> 
    <DataTemplate .... x:name="D1" ....../>
    <DataTemplate .... x:name="D2" ....../>
    <DataTemplate .... x:name="D3" ....../>
    <DataTemplate .... x:name="D4" ....../>
</ResourceDictionary>

Code:

ResoucesDictionary RD = Application.LoadComponent(new Uri("../Views.xaml", UriKind.Relative)) as ResourceDictionary;

if (...condition..)
{
    MyGroupBox.ContentTemplate = RD["D1"] as DataTemplate;
}
else if (....condition...)
{
    MyGroupBox.ContentTemplate = RD["D2"] as DataTemplate;
}
.....
.....
....
.
....
else
{
.......
}

MyGroupBox is an instance of GroupBox in my UI. I want the View of the GroupBox change in run time so that I use DataTemplate.

However, I found that it show the correct UI after > 0.5 seconds whenever a particular condition is triggered.

Is that the performance of DataTemplate so poor? Or is there anything I missed? or wrong?

Thank you very much.

H.B.
  • 166,899
  • 29
  • 327
  • 400
user1184598
  • 449
  • 1
  • 5
  • 11
  • Are you doing any animation during switching of templates? – XiaoChuan Yu Mar 13 '12 at 03:30
  • Most often, data templates are selected in Xaml rather than in code. Are you sure your code is necessary? If you could explain what your conditions are it might help. – Dan Puzey Mar 13 '12 at 14:29
  • Also, you seem to be loading and parsing the content of `Views.xaml` every time you hit that method - which may account for the poor performance. Maybe if you at least cache that value you'd see better results. – Dan Puzey Mar 13 '12 at 14:30

3 Answers3

1

There are a couple of things to ponder ...

  1. Problem could be due to what your data templates are showing. You can have a quick test. Just replace your view inside the data templates with a much simpler and basic view such as bunch of textblocks and textboxes. Does it take same time now? If not then your views are taking time to load and not the data template.

  2. If your views are slow then check if your styles are slow. Are you using software acceleration for the effects such as blur, glow etc? If so then please switch to hardware accelerated effects.

  3. Problem also could be due to the view in side your data templates might be fetching data from backend services / database while the datatemplate loads. Can you delegate this loading of data on a different thread? Because if you dont, then it will execute on GUI and thus hanging the GUI for that while.

  4. Are you showing a list items? Is your items control (basic itemscontrol, listbox, listview, tree view, datagrid) that shows the list of items virtualized? Are you applying any default grouping or sorting on that list? If so, then apply grouping or sorting using LINQ if possible. Use virtualized items controls.

Please check this thread for performance improvements in a WPF GUI.

Community
  • 1
  • 1
WPF-it
  • 19,625
  • 8
  • 55
  • 71
1

I think it's likely your main issue is that you are loading and parsing the content of Views.xaml every time you hit that code - which will affect performance.

If you cache the output of that line between calls, you'd see better results.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
0

Before anything else I would check first if my data bindings are correct.

Just open the debug window and look if you see any binding warnings there. If you are ok with that then the problem is in your views and not the data template. The answer of AngelWPF gives a lot of tips on what to do if this is the case.

Dummy01
  • 1,985
  • 1
  • 16
  • 21
  • Bindings fail pretty quickly and shouldn't generally be a performance issue. Still not a bad idea for general practise, though. – Dan Puzey Mar 13 '12 at 14:36
  • @Dan Puzey I think it strongly depends on your amount of data. If you try to populate for example a ListView with enough records in it, not something crazy, let's say 300 or 500, the difference might be seconds between clear data bindings and ones with errors. – Dummy01 Mar 13 '12 at 14:59
  • I just tested this (I was intrigued)... with 1000 simple items (`DateTime`s) in a non-virtualised list and a duff binding, there was a noticeable hit (about 2 seconds) - but I had to manually disable the virtualization to see it. With virtualisation and only ~40 items on screen, there was no noticeable perf hit. Take from that what you will! :) – Dan Puzey Mar 13 '12 at 15:18