wpf - WPF 绑定(bind)中单引号的用途是什么?

标签 wpf xaml data-binding

类似于 this question ,我很好奇如何解释 WPF 中的表达式 {Binding ''}
请注意,Binding后面有两个撇号。

除了 Google 搜索之外,我还查看了 this link在上面的链接问题中提供,但无法断言绑定(bind)后两个撇号的含义。

我遇到的是字符串格式表达式,其中撇号用在双引号内来表示另一个字符串表达式。但对于这个问题,我怀疑这里的情况是否如此。

这句话是什么意思?

最佳答案

它相当于一个空路径。撇号只是将您在其中写的任何内容括起来。因此,在您的情况下,它是到数据源的绑定(bind)(没有路径) - 尽管我不得不说,我从未见过它以这种方式使用。

您可能在任何与 Bindings 相关的上下文中都没有找到此功能,因为它实际上是所有 XAML 标记扩展(例如 BindingStatic)都可用的功能、StaticResource 等)。

MSDN: Details about how markup extensions are parsed

The text value of either a MEMBERNAME or STRING is read as follows. Leading whitespace characters are consumed without being represented in the generated token. If the first non-whitespace character is a quote (either Unicode code point 0022, Quotation Mark, or 0027, Apostrophe), the tokenizer proceeds as follows:

The first quote is consumed and is not represented in the token’s value.

The text value becomes the characters up to but not including the next matching quote (i.e. a character of the same code point as the opening quote) that is not preceded by a “\” character. All these characters and also the closing quote are consumed. Any “\” characters in the resulting text value are removed.

Whitespace characters following the closing quote are consumed, and are not represented in the token.

以这个简单(而且相当无用)的扩展为例:

public class StringExtension : MarkupExtension
{
    public StringExtension()
    { }

    public StringExtension(string value)
    {
        Value = value;
    }

    public string Value { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return Value;
    }
}

扩展可以像这样使用(所有相同的结果):

<!-- via constructor (1 argument) -->
<TextBlock Text="{local:String text}"/>

<!-- via constructor (1 argument) -->
<TextBlock Text="{local:String 'text'}"/>

<!-- via empty constructor + named property -->
<TextBlock Text="{local:String Value=text}"/>

<!-- via empty constructor + named property -->
<TextBlock Text="{local:String Value='text'}"/>

那么, 的用途是什么?例如用于前导和尾随空格

<!-- no whitespaces -->
<TextBlock Text="{local:String      text    }"/>

<!-- whitespaces -->
<TextBlock Text="{local:String '    text    '}"/>

关于wpf - WPF 绑定(bind)中单引号的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49026929/

相关文章:

c# - 在 WPF 应用程序中以编程方式从 XAML 文件加载矢量图形

wpf - 我可以在不知道绑定(bind)本身的情况下更改 DataTrigger 中绑定(bind)的属性吗?

WPF 绑定(bind)失败性能命中与异常

c# - 将 DataRow 绑定(bind)到 TextBox

data-binding - Asp.Net Core Model绑定(bind),如何获取空字段绑定(bind)为空字符串?

javascript - 在 ascx.cs 用户控件中执行 javascript

wpf - 如何在 WPF XAML 中使用主从与 XML 数据绑定(bind)?

c# - 如何从 DataGrid :Cell to DataTrigger 绑定(bind)值

c# - 如何将属性绑定(bind)到 MAUI 中的 View 模型?

WPF 3D - 为什么我的 ImageBrush 不呈现?