DataTrigger与转换器性能明智的数据绑定 [英] DataTrigger vs databinding with converter performance wise

查看:197
本文介绍了DataTrigger与转换器性能明智的数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的网格定义了大量的样式(基于状态的单元格颜色等)。现在我使用DataTriggers设置适当的颜色。我正在寻求提高我的应用程序的性能,并且认为大量的样式可能会导致速度减慢(由于数据不断变化,样式需要重新计算/刷新)。使用数据绑定与转换器而不是DataTriggers更好吗?



如果使用任一方法可以实现目标,是否有任何性能差异?我不熟悉幕后发生的事情,所以我无法确定。

解决方案

Hanselminutes播客的这一集,嘉宾Laurent Bugnion(MVVM Light的创始人)介绍说,有一个表演命中与使用转换器相关联。他表示在桌面应用程序中几乎总是可以忽略不计,但WP7应用程序中,建议viewmodels直接暴露诸如画笔之类的东西,而不是严重依赖转换器,特别是在处理列表虚拟化时,滚动会导致许多调用转换。



可能类似的事情发生在您的方案中,只要给定触发器的数量(或转换,如果您做了该转换)。 / p>

作为一个概念证明,您可能会考虑将视图模型包含在WPF Viewmodel中,它暴露了颜色和其他WPF特定值本身,而不是一些值可以由运行时调用触发器和 IValueConverters 确定颜色。然后,您可以直接绑定到那个,既不使用触发器也不使用转换器。我有兴趣听到结果。


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.

解决方案

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.

这篇关于DataTrigger与转换器性能明智的数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆