【发布时间】:2009-06-03 20:41:33
【问题描述】:
我遇到的为绑定指定值转换器的最常见方法是:
1. 将值转换器的实例创建为具有键的资源。
2.使用StaticResource标记扩展引用实例:
<TextBlock Text="{Binding Converter={StaticResource myFormatter}" />
问:使用静态实例有什么问题如下:
<TextBlock Text="{Binding Path=Description, Converter={x:Static local:MyFormatter.Instance}}"/>
// where Instance is declared as:
public readonly static MyFormatter Instance = new MyFormatter();
在我的例子中,值转换器是不可变的。
编辑:另一种方式是to turn the converter into an extension 以便您使用标记扩展格式指定转换器:
<TextBlock Text="{Binding Converter={local:MyFormatter}}"/>
【问题讨论】:
-
注意:当你把converter变成扩展时,每次访问都会创建对象。
标签: wpf binding valueconverter