将StringFormat和Converter绑定在一起

Ale*_*ndr 2 data-binding wpf windows-phone-7 windows-phone-8

我从网络服务(User.Avatar)获取用户的头像网址:

 /users/user_id/12?last_update=timestamp
Run Code Online (Sandbox Code Playgroud)

在不同的控件中,我必须使用不同大小的头像(Web服务可以裁剪和调整图像大小):

 ImageSource="{Binding User.Avatar, StringFormat=http://myurl.com/\{0\}/crop/110x110, Converter={StaticResource ImageSizeUrlConverter}}"/>
Run Code Online (Sandbox Code Playgroud)

转换器必须采取

http://myurl.com/users/user_id/12?last_update=timestamp/crop/110x110(带StringFormat)

并返回

http://myurl.com/users/user_id/12/crop/110x110?last_update=timestamp
Run Code Online (Sandbox Code Playgroud)

但转换器采取/users/user_id/12?last_update=timestamp(没有StringFormat).

这是正常的行为吗?

Ton*_*ina 5

好吧,它应该是这样的.您可以将任何值绑定到字符串依赖项属性.转换器是用来从转换类型的目标类型.由于字符串格式化仅适用于字符串,因此在转换器之前进行操作是没有意义的,只能在字之后.

这是一个例子:

{Binding SomeBoolValue,
         StringFormat=You said \{0\},
         Converter={StaticResource BoolToString}}
Run Code Online (Sandbox Code Playgroud)

其中BoolToString返回"是" true和"否" false.格式化必须在将源类型转换为目标类型后进行.

您可以将数据作为ConverterParameter但不能绑定到静态属性的数据发送.您最好的解决方案是在此处创建一个附加属性来执行绑定和转换.