Optimizing DataTemplates

Charles Petzold hit a homerun!  His ‘Writing More Efficient ItemsControls‘ article in the March 2009 issue of MSDN Magazine is the first treatment I’ve ever seen on how to analyze and address performance issues related to the use of DataTemplates and ItemsControls.  He uses empirical evidence to identify performance issues related to a DataTemplate, and shows various ways to improve them.

After reading the article, I am definitely going to go through some templates I’ve created and reassess them.  If you are interested in keeping your WPF applications running quickly, I suggest you check it out.

16 Responses to Optimizing DataTemplates

  1. Chris says:

    This looks very interesting … slowly but surely I am “seeing the light” in regards to datatemplates🙂

    Chris

  2. Josh Smith says:

    Sweet! DataTemplates are magic. Pure magic.

  3. Chris says:

    Fascinating article … I had no idea … especially about performance issues with solidcolorbrushes. I can see where there are thresholds at which performance tweaking pays dividends. Now, I am wondering what are other ways to tweak performance.

    Chris

  4. Chris says:

    I forgot to mention … isn’t it interesting how the MVVM (aka presenter), which was the subject of your recent MSDN article, is an approach that improved performance … it is a great fit for WPF …

  5. Josh Smith says:

    Good point, Chris. Using the MVVM way of thinking in WPF has seemingly limitless benefits.

  6. Josh;

    I am trying to use your sample “Working with Checkboxes in the WPF TreeView” which works great, but I can not figure out how to have the FooViewModel view model that popluates the treeview with a method call to CreateFoos. I want to replace the CreateFoos hard coded data with a call to Ling in an SQL database, but I am missing something. can you provide a sample on replaceing CreateFoos method with a call using Ling to populate the viewmodel and the treeview control in WPF.

    Thank you.

  7. Josh Smith says:

    Danny,

    I am not familiar with LINQ to SQL. You might want to check out some other resources on the Web to learn how to use that.

    Josh

  8. Josh;
    Thanks for your response.
    I can do th LINQ and SQL using a dataset but it only works if I use the DataContext of the Treeview. This by passes your Treeview with Checkboxes and it will so the treeview with checkbox but I loss the functionality you viewmodel provides. (i.e. you can not longer select the root node and it selects the children.)

    I have tried all the other resources include your sample of using the treeview with a dataset. they all work except for the missing using of the viewmodel. I really just need to replace the createFoos() method in your sample that has hard coded data with a foreach loop to cycle thru the dataset and apply the nodes using your viewModel. Is this possible?

  9. Josh Smith says:

    Yes, it’s entirely possible. Where the ViewModel’s data comes from is irrelevant, from an MVVM perspective. The ViewModel is really just a way to decouple the UI from the data model, like a databinding-friendly adapter. If the backing store is a DataSet, that’s fine.

  10. Josh;

    The trouble I am having is replacing the CreateFoo() function. It loads the data upfront when the WPF form loads, I have removed that but need a way to load the dataset after the treeview loads and still use the viewmodel, I can not fine any samples of using a dataset with a viewmodel that still uses a list the rest use ObserverableCollections so your code: this.Children.ForEach(c => c.SetIsChecked(_isChecked, true, false)); does not work because the ObserverableCollection does not have a ForEach.

    Your code works so good that I really want to keep it functionality but need a big kick to point me in the right direction on how to keep you functionality and viewmodel but also use a dataset for the data. I have everything working from a dataset and it loaded the data based on user select topics, but after it loads in the treeview it does not work because I had to bypass your viewmodel.

    Thank You.

  11. Josh Smith says:

    You don’t need to use the .ForEach method. You can just use a foreach loop. The ViewModel classes do not have to follow some prescribed format: it’s totally flexible. It’s just an idea, not a mandate.

  12. Josh;

    Thanks I will try that method. And let you know.

  13. Sandeep says:

    Hi Josh,

    First of all sorry to comment about your MVVM article in this post.

    I am so thankful for you for the MVVM article you wrote in MSDN magazine, it packed such a punch that, I could not get proper sleep for 5 days, I coded and coded, but the hunger dint go away.

    Your article not only taught me MVVM, but also to be a disciplined WPF programmer, I was unneccesarily cluttering the entire app.

    After reading your article, my love for WPF just got doubled.

    Thank you, thanks a lot..

  14. Josh Smith says:

    Wow. Thanks a lot, Sandeep. That’s the best feedback I’ve ever received!😀

    Josh

  15. Sandeep says:

    Oh my gosh, oh my gosh, thanks a lot Josh,
    tat really made my day, (infact many coming months)

    Warm regards,
    Sandeep

%d bloggers like this: