简体   繁体   English

带转换器的多重绑定中的 XAML 字符串格式

[英]XAML string format in multibinding w/converter

I am implementing this TextBlock and the stringformat is not appearing, only the value of the binding property.我正在实现这个 TextBlock 并且stringformat没有出现,只有 binding 属性的值。 Can you tell me what I'm doing wrong?你能告诉我我做错了什么吗?

XAML CODE 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>

Converter转换器

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;
    }
}

Best regards,此致,

See the Remarks section on the StringFormat page on MSDN:请参阅 MSDN 上StringFormat页面上的备注部分:

... ...

When you use a MultiBinding, the StringFormat property applies only when it is set on the MultiBinding.使用 MultiBinding 时,StringFormat 属性仅适用于在 MultiBinding 上设置的情况。 The value of StringFormat that is set on any child Binding objects is ignored.在任何子 Binding 对象上设置的 StringFormat 值将被忽略。 ... ...

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


So you either set the StringFormat of the MultiBinding (and don't set its Converter ), or you do the formatting in your converter.因此,您要么设置 MultiBinding 的StringFormat (并且不设置其Converter ),要么在转换器中进行格式化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM