是“{Binding Path=.}”吗?和“{绑定}”真正平等
在我的 WPF 项目中,我有一个 ListBox,它显示 List
集合中的项目。我想让这些项目的文本可编辑,因此我将它们每个都包装在带有 TextBox 的 ItemTemplate 中(可能不是最好的方法,但我是 WPF 新手)。我在简单地将 TextBoxes 的 Text 属性绑定到每个项目的值时遇到了麻烦。我最终偶然发现了一个使用单个点或句点作为 Path 属性的示例 ({Binding Path=.}
):
<ListBox ItemsSource="{Binding ElementName=recipesListbox,Path=SelectedItem.Steps}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=.}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
但是我不明白为什么只使用 {Binding}
没用。
它引发了“双向绑定需要 Path 或 XPath”异常,如 根据 Microsoft:
[...] 句点 (.) 路径可用于绑定到当前源。例如,Text="{Binding}" 相当于 Text="{Binding Path=.}"
有人能阐明这种模棱两可的行为吗?
编辑:此外,似乎 {Binding Path=.}
不一定提供双向绑定,因为修改文本和移动焦点不会更新底层源(同一源还在 DataGrid 控件上显示并成功修改了属性)。我肯定在这里遗漏了一些东西。
In my WPF project, I have a ListBox that displays items from a List<string>
collection. I wanted to make the text of these items editable, so I wrapped each of them in an ItemTemplate with a TextBox (might not be the best way, but I'm new to WPF). I was having trouble simply binding the TextBoxes' Text property to the value of each item. I finally stumbled upon an example using a single dot or period for its Path property ({Binding Path=.}
):
<ListBox ItemsSource="{Binding ElementName=recipesListbox,Path=SelectedItem.Steps}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=.}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
However I don't understand why simply using {Binding}
didn't work.
It raised a "Two-way binding requires Path or XPath" exception, as according to Microsoft:
[...] a period (.) path can be used to bind to the current source. For example, Text="{Binding}" is equivalent to Text="{Binding Path=.}"
Could someone shed light on this ambiguous behavior?
EDIT: Moreover, it seems {Binding Path=.}
does not necessarily give two-way binding, as modifying the text and moving the focus does not update the underlying source (the same source has also properties displayed and successfully modified on a DataGrid control). I'm definitely missing something here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
例外的要点大概是您无法双向绑定绑定源本身,因此它会尝试阻止您创建一个行为方式与您希望的方式不同的绑定。通过使用
{Binding Path=.}
,您只是欺骗了错误检测。(此外,文档错误或不准确的情况也并非闻所未闻,尽管我总体上非常喜欢 MSDN 文档,因为它通常包含人们感兴趣的关键点)
The point of the exception presumably is that you cannot two-way bind a binding-source itself, so it tries to prevent you from creating a binding which does not behave the way you would want it to. By using
{Binding Path=.}
you just trick the error detection.(Also it's not unheard of that documentation is erroneous or inaccurate, though i do like the MSDN documentation a lot in general as it usually does contain the crucial points one is interested in)
文档指出
{Binding}
相当于{Binding Path=.}
。但是,它不等于您键入的{Binding Path}
。如果包含Path
属性,则必须将其分配给某个内容,可以是Path=.
或Path=OtherProperty
。The documentation states that
{Binding}
is equivalent to{Binding Path=.}
. However it is not equivalent to{Binding Path}
as you have typed. If you include thePath
property, you must assign it to something, be itPath=.
orPath=OtherProperty
.简而言之,两者的区别类似于传统的按值传递和按引用传递的区别。 (FYR - 通过引用传递有什么区别与按值传递?)
现在让我们假设
{Binding}
可以是用于双向绑定。一般来说,{Binding}
创建一个带有数据上下文的基于值的链接,该链接不允许更新数据上下文。而
{Binding Path=.}
使用“Path”引用的内存区域创建基于引用的链接,允许通过引用更新值。(在本例中,“点”是当前数据上下文)。希望这有帮助!
In short, the difference between the two is analogous with the difference between the traditional pass by value and pass by reference. (FYR - What's the difference between passing by reference vs. passing by value?)
Lets assume here for now that
{Binding}
can be used for two way binding. In general{Binding}
creates a value based link with datacontext which does not allow updating the datacontext.Whereas
{Binding Path=.}
creates reference based link with the memory area referenced by the 'Path' which allows updating the value through reference.(in this case 'dot' the current datacontext).Hope this helps!
这些不一样。如果您在 ConsoleMessages 是仅带有 {Binding} 的 ObservableCollection 字符串的情况下绑定它,您会得到“双向绑定需要 Path 或 XPath”。异常,其中 {Binding Path=.} 有效。这是 WPF 4.0...
我的 2p 值...
These are not the same. If you bind this where ConsoleMessages is an ObservableCollection string with just {Binding} you get a "Two-way binding requires Path or XPath." exception where as {Binding Path=.} works. This is with WPF 4.0...
My 2p worth...