无法在 XAML 绑定的 StringFormat 中使用撇号?
我正在尝试使用 StringFormat 在绑定到 TextBlock 的值周围插入撇号(撇号?):
<TextBlock Text="{Binding MyValue, StringFormat='The value is '{0}''}"/>
但是,我收到编译错误:
MarkupExtension 中的名称和值不能包含引号。 MarkupExtension 参数“MyValue, StringFormat=”值为“{0}”“}”无效。
我确实注意到它确实适用于引号:
<TextBlock Text="{Binding MyValue, StringFormat='The value is "{0}"'}"/>
Is this a bug with StringFormat?
I'm trying use StringFormat to insert apostrophies (apostrophe's?) around a value that is bound to a TextBlock:
<TextBlock Text="{Binding MyValue, StringFormat='The value is '{0}''}"/>
However, I get a compile error:
Names and Values in a MarkupExtension cannot contain quotes. The MarkupExtension arguments ' MyValue, StringFormat='The value is '{0}''}' are not valid.
I do notice that it does work for quotes though:
<TextBlock Text="{Binding MyValue, StringFormat='The value is "{0}"'}"/>
Is this a bug with StringFormat?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确定这是否是一个错误,但我测试了这个方法,它有效:
似乎 StringFormat 中的单引号必须使用
\
进行转义,而不是传统的 XML 样式& ;apos;
I'm not sure if it's a bug, but I tested this method, and it works:
Seems like single quotes within StringFormat have to be escaped using
\
as opposed to the traditional XML style'
尝试在
&apos
之前使用\
:Try to use
\
before&apos
:这唯一的解决方案对我有用:删除 FallbackValue 引号 (!),然后转义特殊字符。
连 VS2017 XAML Intellisense 都丢失了!它以蓝色显示“它”,以红色显示“可以”,以蓝色显示“更奇怪”......但它有效。
我什至测试了这个更复杂的情况,并且带有空格且不带引号的文本后面的属性被正确解释:(
在 VS2017,Framework 4.0 上测试)
This only solution worked for me : remove FallbackValue quotes (!) and then escape the special character.
Even the VS2017 XAML Intellisense is lost ! it displays "It" in blue, "couldn" in red, and "be more weird" in blue... but it works.
I even tested this more complexe case, and attributes following a text with spaces and without quotes are correctly interpreted :
(Tested on VS2017, Framework 4.0)
刚刚在 UWP 为 TimeSpan 值创建 StringFormatConverter ConverterParameter 时遇到了这个问题。由于某些疯狂的原因,TimeSpan.ToString(x) 自定义格式字符串需要在文字字符周围使用撇号,即使 DateTimeOffset 不需要。看起来毫无必要的不一致。
无论如何......以上都没有成功。一种方法可以让 XAML 设计器显示/工作,但它会产生构建错误。
我决定的解决方案是将格式字符串放入最近的封闭元素的字符串资源中。由于我在使用 Border 创建的框中显示值,因此我将格式字符串填充到 Border 元素的资源中。
StringFormatConverter 是 NuGet 上的 Microsoft UWP 工具包中的一个。
Just encountered this problem for UWP creating a StringFormatConverter ConverterParameter for a TimeSpan value. The TimeSpan.ToString(x) custom format string for some crazy reason requires apostrophes around literal characters even though DateTimeOffset doesn't. Seems needlessly inconsistent.
Anyway... None of the above successfully worked. One approach worked as far as getting the XAML designer to display/work, but it created a build error.
The solution I settled upon was to put the format string in a string resource of the nearest enclosing element. Since I was displaying the value in a box created using Border, I stuffed the format string into the resources of the Border element.
The StringFormatConverter is the one from the Microsoft UWP Toolkit on NuGet.