gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:21:40 25 4
gpt4 key购买 nike

我有这个文本 block

   <TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}, {1}, {2}, ">
<Binding Path="object.strProp1" />
<Binding Path="object.strProp2" />
<Binding Path="object.strProp3" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>

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

这个的输出将是这样的:

strProp1, strProp2, strProp3,

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

有什么建议吗?谢谢!编辑:最好只使用 xaml :)

最佳答案

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

因此您的绑定(bind)将是:

<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}\, {1}\, {2}\, ">
<Binding Path="object.strProp1" />
<Binding Path="object.strProp2" />
<Binding Path="object.strProp3" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>

关于wpf - 在 wpf 中的 StringFormat 中向字符串添加逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30044649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com