【问题标题】:What does "{Binding Path=.}" mean in WPF binding?WPF 绑定中的“{Binding Path=.}”是什么意思?
【发布时间】:2010-11-07 03:59:04
【问题描述】:

{Binding Path=.} 在 WPF 绑定中是什么意思?

我看到有人使用它,但找不到任何解释。

绑定语法中是否还有其他特殊符号({Binding /} 除外)?

【问题讨论】:

    标签: wpf data-binding xaml


    【解决方案1】:

    几个月前我发现了这个WPF Binding CheatSheet,发现它非常有用,尤其是对于任何学习 WPF 的人。里面有一些拼写错误,但还是不错的。

    这是一小段摘录(应该是表格格式):

    Basic Binding
    {Binding} Bind to current DataContext.
    {Binding Name} Bind to the “Name” property of the current DataContext.
    {Binding Name.Length} Bind to the Length property of the object in the Name property of the current DataContext.
    {Binding ElementName=SomeTextBox, Path=Text} Bind to the “Text” property of the element XAML element with name=”SomeTextBox” or x:Name=”SomeTextBox”.

    Direct link to CheatSheet

    【讨论】:

    • 不错,但 {Binding .} 是什么。我正在寻找这个解释,但我找不到?
    • @batmaci 我想说它相当于{Binding },但是我已经有好几年没有使用 Xaml 了,所以我不确定。
    • @batmaci,多年后...{Binding string} 类似于{Binding Path=string}string 不用于直接初始化属性Path,而是作为构造函数Binding (string path) 的参数,然后初始化Path 属性。这仅在字符串是Binding 之后的第一个标记时才有效,其余的是常规初始化程序(属性=值对)。知道这一点,Binding . 实际上等于Binding Path=. 并且属性路径语法可用here
    【解决方案2】:

    这是绑定到当前源的简写。欲了解更多信息,请参阅here

    具体来自文档:

    (可选)句点 (.) 路径可用于绑定到当前 资源。例如,Text="{Binding}" 等价于 Text="{Binding Path=.}"

    【讨论】:

    • @Ray - 我的想法完全正确。如果你要速记,为什么要停在那里?
    • 我无法与 MSDN 争论,但还是有区别的。 {Binding} 不能用于对象本身(需要属性的两种方式),而 {Binding Path=.} 有效。
    • @Sergey - 您是否有一个代码示例,其中 {Binding} 不起作用但 {Binding Path=.} 起作用?如果是这样,MSDN 可能需要更新...
    • Easily: var s = "test; DataContext=s; and in - 不起作用。但 MSDN 不是 wiki...
    • 另一个例子: 不起作用,因为您必须设置路径。在这种情况下,路径 =。很有用。
    猜你喜欢
    • 1970-01-01
    • 2013-01-04
    • 2014-03-19
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    • 2022-10-18
    相关资源
    最近更新 更多