如何转义 XAML 标记扩展属性文本中的单引号?

发布于 2024-08-02 10:06:18 字数 653 浏览 16 评论 0原文

我有一个可以格式化数字的值转换器(不幸的是我还不能使用 SP1)。它工作正常,直到获得百分比。

下面是一个示例:

<TextBlock Text="{Binding Path=PercentageComplete,
                          Converter={StaticResource NumberFormatter},
                          ConverterParameter='0.00 %'}" />

不幸的是,当 Double.ToString 看到百分比字符时,它会将数字乘以 100。在我的例子中,数字已经是百分比,不需要转换。

在 C# 中,这可以通过使用单引号转义 % 字符来实现:

(99.99).ToString("0.00 %")  // gives -> "9999 %"
(99.99).ToString("0.00 '%") // gives -> "99.99 %"

不幸的是,我无法在上述 XAML 标记扩展的 ConverterParameter 中使用单引号。有办法逃避吗?我尝试过加倍单引号并使用反斜杠,但都无法编译。

I have a value converter that formats numbers (I can't use SP1 yet unfortunately). It works fine until it gets a percentage.

Here's an example:

<TextBlock Text="{Binding Path=PercentageComplete,
                          Converter={StaticResource NumberFormatter},
                          ConverterParameter='0.00 %'}" />

Unfortunately for me when Double.ToString sees a percentage character, it multiplies the number by 100. In my case, the number is already a percentage and no conversion is needed.

In C#, this would be achieved by escaping the % character with a single quote:

(99.99).ToString("0.00 %")  // gives -> "9999 %"
(99.99).ToString("0.00 '%") // gives -> "99.99 %"

Unfortunately, I cannot use a single quote in the ConverterParameter in the above XAML markup extension. Is there a way of escaping it? I have tried doubling the single quotes and using a backslash, but both failed to compile.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

廻憶裏菂餘溫 2024-08-09 10:06:18

未经测试,但您尝试过:

<TextBlock Text="{Binding Path=PercentageComplete,
                      Converter={StaticResource NumberFormatter},
                      ConverterParameter="0.00 '%"}" />

Untested, but have you tried:

<TextBlock Text="{Binding Path=PercentageComplete,
                      Converter={StaticResource NumberFormatter},
                      ConverterParameter="0.00 '%"}" />
夜血缘 2024-08-09 10:06:18

以下内容摘自 http://msdn.microsoft.com/en-us/库/ee200269.aspx

您可以使用反斜杠转义任何字符。它不会解析反斜杠,而只是跳过它们。因此:

  • 'foo\'bar' 变为foo'bar
  • "foo\"bar" 变为 foo"bar;
  • 'foo\\bar' 变为 foo\bar
  • 'foo\nbar' 变为 foonbar 而不是换行符。

然而,标记扩展解析相当奇怪。尽管 Visual Studio 语法突出显示不支持它,但除 \{},= 之外的任何字符都是有效的值字符。这意味着以下结构是合法的:

  • {MyExtension Name=foo'bar} (引号必须是被视为引用字符串的第一个字符;其他任何地方都只是逐字复制);
  • {MyExtension Name=f oo} (空格也是合法的;这会变成 f oo);
  • {MyExtension Name= foo } (值周围的空格被修剪;这变成 foo);
  • {MyExtension Name=foo\\bar}\ 之后的字符被逐字复制,因此变成 foo\bar);
  • {MyExtension Name=foo \\ bar} (这变成 foo \ bar);
  • {MyExtension Name=foo \} bar} (这变成 foo } bar)。

请注意,\ 规则也适用于此处:\ 后面的任何字符都会逐字复制。

The below is taken from http://msdn.microsoft.com/en-us/library/ee200269.aspx.

You can escape any character with a backslash. It does not parse the backslashes but just skips them. So:

  • 'foo\'bar' becomes foo'bar;
  • "foo\"bar" becomes foo"bar;
  • 'foo\\bar' becomes foo\bar;
  • 'foo\nbar' becomes foonbar and not a newline character.

However, markup extension parsing is quite strange. Even though Visual Studio syntax highlighting doesn't support it, any character other than \{},= is a valid value character. This means that the following constructions are legal:

  • {MyExtension Name=foo'bar} (quotes must be the first character to be considered quoting for a string; anywhere else its just copied verbatim);
  • {MyExtension Name=f oo} (spaces are legal too; this becomes f oo);
  • {MyExtension Name= foo } (spaces around the value are trimmed; this becomes foo);
  • {MyExtension Name=foo\\bar} (characters after a \ are copied verbatim, so this becomes foo\bar);
  • {MyExtension Name=foo \\ bar} (this becomes foo \ bar);
  • {MyExtension Name=foo \} bar} (and this becomes foo } bar).

Note that the \ rules apply here too: any character following a \ is copied verbatim.

鸵鸟症 2024-08-09 10:06:18

您可以使用 String.Format 而不是 Double.ToString

public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture ) {
    string format = (string) parameter;

    return String.Format( format, value );
}

并且在绑定表达式中,使用特殊的 {} 转义序列:

<TextBlock Text="{Binding PercentageComplete, Converter={StaticResource NumberFormatter}, ConverterParameter='{}{0:0.00} %'}"></TextBlock>

You can use String.Format instead of Double.ToString

public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture ) {
    string format = (string) parameter;

    return String.Format( format, value );
}

And in your binding expression, use the special {} escape sequence:

<TextBlock Text="{Binding PercentageComplete, Converter={StaticResource NumberFormatter}, ConverterParameter='{}{0:0.00} %'}"></TextBlock>
怀中猫帐中妖 2024-08-09 10:06:18

这是避免标记扩展的解决方法,尽管它不是问题的直接答案。

<TextBlock>
  <TextBlock.Text>
    <Binding Path="PercentageComplete"
             Converter="{StaticResource NumberFormatter}"
             ConverterParameter="0.00 '%" />
  </TextBlock.Text>
</TextBlock>

Here is a workaround that avoids the markup extension, though it isn't a direct answer to the question.

<TextBlock>
  <TextBlock.Text>
    <Binding Path="PercentageComplete"
             Converter="{StaticResource NumberFormatter}"
             ConverterParameter="0.00 '%" />
  </TextBlock.Text>
</TextBlock>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文