在 wpf 中的 StringFormat 内向字符串添加逗号

Mac*_*ito 2 wpf xaml wpf-controls

我有这个文本块

   <TextBlock>
      <TextBlock.Text>
        <MultiBinding StringFormat="{}{0}, {1}, {2}, ">
          <Binding Path="object.strProp1" />
          <Binding Path="object.strProp2" />
          <Binding Path="object.strProp3" />
        </MultiBinding>
      </TextBlock.Text>
    </TextBlock>
Run Code Online (Sandbox Code Playgroud)

假设object不为 null 且*strProp1* = "strProp1"*strProp2* = "strProp2"、 和*strProp2* = "strProp2"

其输出将是这样的:

strProp1, strProp2, strProp3,
Run Code Online (Sandbox Code Playgroud)

我想知道的是,每当对象为空或属性之一为空时,如何删除“,”。也就是说,如果object为 null,则 Textblock 将为空。或者,如果其中一个对象是空的,那么它就只是空的。

关于如何做到这一点有什么建议吗?谢谢!编辑:最好仅在 xaml 中:)

小智 5

我知道这是一个老问题,但我正在做类似的事情并提出了这个解决方案。您只需转义“,”,就像使用“\”转义字符串中的特殊字符一样。

所以你的绑定将是:

<TextBlock>
   <TextBlock.Text>
     <MultiBinding StringFormat="{}{0}\, {1}\, {2}\, ">
       <Binding Path="object.strProp1" />
       <Binding Path="object.strProp2" />
       <Binding Path="object.strProp3" />
     </MultiBinding>
   </TextBlock.Text>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)