为什么我的 IMultiBindingConverter 在用于设置 TextBox.Text 时会得到一个字符串数组?
我正在尝试将 MultiBinding 与转换器一起使用,其中子元素也有一个转换器。
XAML 看起来像这样:
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource localizedMessageConverter}" ConverterParameter="{x:Static res:Resources.RecordsFound}" >
<Binding Converter="{StaticResource localizedMessageParameterConverter}" ConverterParameter="ALIAS" Path="Alias" Mode="OneWay" />
<Binding Converter="{StaticResource localizedMessageParameterConverter}" ConverterParameter="COUNT" Path="Count" Mode="OneWay" />
</MultiBinding>
</TextBlock.Text>
我在这里面临的问题是,每当与 TextBlock 一起使用来指定 Text 属性时,我的 IMultiValueConverter 实现都会获取字符串的对象集合,而不是 IValueConverter 返回的类。似乎对内部转换器的结果调用了 ToString() 方法并将其传递给 IMultiValueConverter。如果用来指定Label的Content属性,一切都很好。
在我看来,框架假设返回类型为字符串,但为什么呢?我可以在 MultiBinding 中看到这一点,因为它应该产生与 TextBlock.Text 兼容的结果,但为什么 MultiBinding 内的 Bindings 也会出现这种情况?
如果我从内部 Binding 元素中删除转换器,则会返回本机类型。就我而言,字符串和整数。
I'm trying to use a MultiBinding with a converter where the child elements also have a converter.
The XAML looks like so:
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource localizedMessageConverter}" ConverterParameter="{x:Static res:Resources.RecordsFound}" >
<Binding Converter="{StaticResource localizedMessageParameterConverter}" ConverterParameter="ALIAS" Path="Alias" Mode="OneWay" />
<Binding Converter="{StaticResource localizedMessageParameterConverter}" ConverterParameter="COUNT" Path="Count" Mode="OneWay" />
</MultiBinding>
</TextBlock.Text>
The problem I'm facing here is, whenever this is used with a TextBlock to specify the Text property, my IMultiValueConverter implementation gets an object collection of strings instead of the class returned by the IValueConverter. It seems that the ToString() method is called on the result of the inner converter and passed to the IMultiValueConverter. If used to specify the Content property of Label, all is well.
It seems to me that the framework is assuming that the return type will be string, but why? I can see this for the MultiBinding since it should yield a result that is compatible with TextBlock.Text, but why would this also be the case for the Bindings inside a MultiBinding?
If I remove the converter from the inner Binding elements, the native types are returned. In my case string and int.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
localizedMessageParameterConverter 转换器的 targetType 参数可能是 System.String。这是因为Bindings的目标类型是从MultiBinding继承的,而MultiBinding的targetType是System.String,因为TextBlock.Text是一个字符串属性。
有关类似问题,请参阅以下文章:多值转换器,值转换器和错误目标类型的情况
根据 Microsoft Connect,此问题已在 WPF 4.0 中修复。请参阅: Microsoft Connect
上面的文章还解释了一种解决方法。
Probably the targetType parameter of your localizedMessageParameterConverter converter is System.String. This is because the target type of the Bindings is inherited from the MultiBinding, and the targetType of the MultiBinding is System.String because TextBlock.Text is a string property.
See the following article for a similar problem: Multi-Value Converters, Value Converters and the Case of the Wrong Target Type
According to Microsoft Connect, this has been fixed in WPF 4.0. See: Microsoft Connect
The above article also explains a workaround.