wpf 样式转换器:“转换”由使用它的每个数据网格列调用
我创建了一个转换器,并将其分配给一种样式。
比我将该样式分配给我想要受影响的列。
当添加行时,在单步执行调试器时,我注意到转换器转换方法每列被调用 1 次(每次使用时)。
有没有一种方法可以更好地优化它,以便它仅被调用一次并且使用它的所有列都获得相同的值?
<Style x:Key="ConditionalColorStyle" TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource CellStyle}">
<Setter Property="Foreground">
<Setter.Value>
<Binding>
<Binding.Converter>
<local:ConditionalColorConverter />
</Binding.Converter>
</Binding>
</Setter.Value>
</Setter>
</Style>
I created a converter, and assigned it to a style.
than i assigned that style, to the columns i want affected.
as rows are added, and while stepping through debugger, i noticed that the converter convert method gets called 1 time per column (each time it is used).
is there a way to optimize it better, so that it gets called only once and all columns using it get the same value?
<Style x:Key="ConditionalColorStyle" TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource CellStyle}">
<Setter Property="Foreground">
<Setter.Value>
<Binding>
<Binding.Converter>
<local:ConditionalColorConverter />
</Binding.Converter>
</Binding>
</Setter.Value>
</Setter>
</Style>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您可以将转换器的结果缓存在成员变量中。
Perhaps you could cache the result of the converter in a member variable.
我意识到我为每个单元格设置的属性也可以设置为行。所以我将转换器分配给该行。
设置了这些样式的单元格不会受到转换器的影响,因为单元格样式优先于行样式。
因此,单元格级别的灵活性与为每列执行的转换之间存在一些权衡。
i realized that the properties i am setting to each of the cells, can also be set to the row. so i assigned the converter to the row.
The cells that do have these styles set, will NOT be affected by the converter, as cell style takes precedence over row style.
so there is some trade off in cell level flexibility vs Convert executed for each column.