我正在考虑在 XAML 中推进 C# 6 字符串插值的方法,例如在一些简单的场景中使用它们而不是值转换器,例如在绑定(bind)到数字时用空字符串替换零。
来自其design discussions :
An interpolated string is a way to construct a value of type String (or IFormattable) by writing the text of the string along with expressions that will fill in "holes" in the string. The compiler constructs a format string and a sequence of fill-in values from the interpolated string.
但是,正如我所怀疑的那样,它们似乎不能从 XAML 中使用,因为它使用不同的编译器来生成 BAML,而且我在生成的
.g.i.cs
中找不到字符串的痕迹。文件。最佳答案
这听起来很像 .Net 3.5 中引入的 StringFormat 属性。正如您所引用的,“编写字符串的文本以及将填充字符串中的“孔”的表达式”,这可以在 XAML 绑定(bind)中执行,如下所示:
<TextBlock Text="{Binding Amount, StringFormat=Total: {0:C}}" />
由于您可以使用任何自定义字符串格式,因此这里有很多强大的功能。还是你在问别的?
关于wpf - XAML 中的字符串插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30459680/