wpf "{Binding Path=.}" 和 "{Binding}" 真的相等吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5488014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are "{Binding Path=.}" and "{Binding}" really equal
提问by Fueled
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=.}
):
在我的 WPF 项目中,我有一个 ListBox,用于显示List<string>
集合中的项目。我想让这些项目的文本可编辑,所以我将它们每个都包裹在一个带有 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>
However I don't understand why simply using {Binding}
didn't work.
但是我不明白为什么简单地使用{Binding}
不起作用。
It raised a "Two-way binding requires Path or XPath" exception, as according to Microsoft:
根据 Microsoft 的说法,它引发了“双向绑定需要 Path 或 XPath”异常:
[...] a period (.) path can be used to bind to the current source. For example, Text="{Binding}" is equivalent to Text="{Binding Path=.}"
[...] 句点 (.) 路径可用于绑定到当前源。例如,Text="{Binding}" 等价于 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.
编辑:此外,似乎{Binding Path=.}
不一定要提供双向绑定,因为修改文本和移动焦点不会更新底层源(同一源也有在 DataGrid 控件上显示和成功修改的属性)。我肯定在这里遗漏了一些东西。
回答by H.B.
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 handling.
例外的一点大概是你不能双向绑定绑定源本身,所以它试图阻止你创建一个行为不符合你希望的绑定。通过使用{Binding Path=.}
你只是欺骗错误处理。
(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)
(此外,该文档是错误或不准确的并不是闻所未闻,尽管我确实非常喜欢 MSDN 文档,因为它通常包含人们感兴趣的关键点)
回答by ceyko
The documentation states that {Binding}
is equivalent to {Binding Path=.}
. However it is notequivalent to {Binding Path}
as you have typed. If you include the Path
property, you must assign it to something, be it Path=.
or Path=OtherProperty
.
文档说明这{Binding}
等效于{Binding Path=.}
. 但是,它不等同于{Binding Path}
您键入的内容。如果您包含该Path
属性,则必须将其分配给某物,无论是 itPath=.
还是Path=OtherProperty
。
回答by ceyko
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...
这些是不一样的。如果在 ConsoleMessages 是一个 ObservableCollection 字符串的地方绑定它,只有 {Binding} 你会得到一个“双向绑定需要 Path 或 XPath”。{Binding Path=.} 工作的异常。这是 WPF 4.0 ...
<ItemsControl x:Name="ConsoleOutput" ItemsSource="{Binding ConsoleMessages, Mode=OneWay}" MaxHeight="400">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=.}" BorderThickness="0" Margin="0" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
My 2p worth...
我的 2p 值...
回答by Vwake
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?)
简而言之,两者的区别类似于传统的传值和传引用的区别。(FYR -按引用传递与按值传递有什么区别?)
However I don't understand why simply using {Binding} didn't work (it raised a "Two-way binding requires Path or XPath" exception)
但是我不明白为什么简单地使用 {Binding} 不起作用(它引发了“双向绑定需要 Path 或 XPath”异常)
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.
现在让我们假设它{Binding}
可用于双向绑定。通常{Binding}
,使用不允许更新数据上下文的数据上下文创建基于值的链接。
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).
而{Binding Path=.}
使用“路径”引用的内存区域创建基于引用的链接,这允许通过引用更新值。(在这种情况下,“点”当前数据上下文)。
Hope this helps!
希望这可以帮助!