DataTrigger vs databinding with converter performance wise
Asked Answered
S

2

22

I have a large number of styles defined for my grid (cell colors based on state, etc.). Right now I use DataTriggers to set the appropriate colors. I am looking to improve the performance of my app and was thinking that the large number of styles may be causing a slow down (due to the data changing constantly and the styles needing to be recalculated/refreshed). Is it better to use data binding with converters instead of DataTriggers?

If the goal can be achieved using either method, is there any performance difference? I am not familiar with what is going on behind the scenes so I can't be sure.

Swaine answered 1/4, 2011 at 0:59 Comment(0)
F
19

In this episode of the Hanselminutes podcast, guest Laurent Bugnion (creator of MVVM Light) describes that there is a performance hit associated with using converters. He indicates that it is almost always negligible in desktop applications, but that in WP7 applications, it is recommended that viewmodels expose things like brushes directly, rather than relying heavily on converters, especially if dealing with list virtualization, in which case scrolling will cause many invocations of the conversion.

It is possible that a similar thing is occurring in your scenario, just given the volume of triggers (or conversions if you made that switch).

As a proof of concept, you might consider wrapping your view-model in a "WPF Viewmodel," that exposes the colors and other WPF-specific values themselves, rather than some value from which the colors can be determined by the runtime invoking triggers and IValueConverters. You'll then be able to bind directly to that, and use neither triggers nor converters. I'd be interested to hear the results.

Firebrat answered 1/4, 2011 at 4:28 Comment(0)
M
0

In our software I changed 2000 data bindings that were using converters with datatriggers in the style. (VisibleIfTrueConverters mostly) Unfortunately it didn't make any performance difference. Not even in ItemsControls and ListBoxes with a huge amount of controls that could be Visible or Collapsed. The view loaded 8 seconds either way. So I undid the changes in order to have less lines of code.

Myrtice answered 2/3, 2023 at 15:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.