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

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

问题描述

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

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.

推荐答案

这一集的 Hanselminutes播客,来宾 Laurent Bugnion(MVVM Light 的创建者)描述了使用转换器会导致性能下降.他表示在桌面应用中几乎总是可以忽略不计,但是在WP7应用中,建议viewmodels直接暴露画笔之类的东西,而不是过度依赖转换器,特别是在处理列表虚拟化时,在这种情况下滚动会导致很多调用转换.

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).

作为概念证明,您可以考虑将您的视图模型包装在WPF 视图模型"中,该模型公开颜色和其他 WPF 特定值本身,而不是运行时可以从中确定颜色的某些值调用触发器和 IValueConverters.然后您将能够直接绑定到它,并且既不使用触发器也不使用转换器.我很想知道结果.

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天全站免登陆