带有转换器的多绑定的XAML字符串格式 [英] XAML string format in multibinding w/converter

查看:93
本文介绍了带有转换器的多绑定的XAML字符串格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现此TextBlock,并且没有出现 stringformat ,只有绑定属性的值出现了.你能告诉我我在做什么错吗?

I am implementing this TextBlock and the stringformat is not appearing, only the value of the binding property. Can you tell me what I'm doing wrong?

XAML代码

<TextBlock>
     <TextBlock.Text>
       <MultiBinding Converter="{StaticResource ResourceKey=myConverter}">
         <Binding Path="loc.country" StringFormat="Country: {0}"/>
         <Binding Path="loc.area" StringFormat="Area: {0}"/>
       </MultiBinding>
     </TextBlock.Text>
 </TextBlock>

转换器

public class MyMultiConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values[1] == null)
            return values[0];

        return values[1];
    }

    public object[] ConvertBack(object values, Type[] targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

最诚挚的问候,

推荐答案

请参阅

...

...

当您使用MultiBinding时,将应用StringFormat属性 仅当在MultiBinding上设置它时. StringFormat的值 在任何子Binding对象上设置的值将被忽略. ...

When you use a MultiBinding, the StringFormat property applies only when it is set on the MultiBinding. The value of StringFormat that is set on any child Binding objects is ignored. ...

原因是StringFormat仅在绑定的目标属性实际上是string类型时才应用,而在MultiBinding中则不是这种情况

The reason is that StringFormat is only applied when the target property of the binding is actually of type string, which is not the case in a MultiBinding

因此,您可以设置MultiBinding的StringFormat(而不设置其Converter),或者在转换器中进行格式化.

So you either set the StringFormat of the MultiBinding (and don't set its Converter), or you do the formatting in your converter.

这篇关于带有转换器的多绑定的XAML字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆