wpf - XAML 中的字符串插值

标签 wpf xaml string-interpolation markup-extensions c#-6.0

我正在考虑在 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 中找不到字符串的痕迹。文件。
  • XAML 不支持字符串插值吗?
  • 可能有什么解决方法?也许使用标记扩展来动态编译字符串插值?
  • 最佳答案

    这听起来很像 .Net 3.5 中引入的 StringFormat 属性。正如您所引用的,“编写字符串的文本以及将填充字符串中的“孔”的表达式”,这可以在 XAML 绑定(bind)中执行,如下所示:

    <TextBlock Text="{Binding Amount, StringFormat=Total: {0:C}}" />
    

    由于您可以使用任何自定义字符串格式,因此这里有很多强大的功能。还是你在问别的?

    关于wpf - XAML 中的字符串插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30459680/

    相关文章:

    scala - 在 scala 的字符串插值中循环

    c# - WPF Popup 有一个黑色边框而不是透明边框

    c# - 使用 Kinect 在 InkCanvas WPF 中绘制平滑线条

    c# - 从代码设置自定义 MarkupExtension

    c# - 在Silverlight控件中支持文本内容吗?

    ruby - 在 Ruby 中,插入字符串的数据是否会导致字符串终止?

    boxing - C# 6 字符串插值是否像 string.Format() 那样对其参数使用装箱?

    c# - 移除 Storyboard,使其动画回到原来的状态

    c# - "Property value is not valid."为什么 visual studio 不允许我将图片分配给图像?

    c# - 尝试访问资源时出现 XamlParseException