WPF 绑定到自身

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1906587/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:19:58  来源:igfitidea点击:

WPF Bind to itself

wpfdata-bindingbindingself

提问by Snake

I've got a WPF Window, and somewhere there is a ListViewwhere I bind a List<string>to.

我有一个 WPF Window,在某个地方有一个ListView我将 a 绑定List<string>到的地方。

Now somewhere in my ListViewthere is a TextBoxand the Contentproperty is set to {Binding}.

现在在我的某个地方ListView有一个TextBox并且Content属性设置为{Binding}.

But this is the shorthand. How do I write the full binding to bind to itself?

但这是简写。如何编写完整绑定以绑定到自身?

{Binding Path=Self}doesn't work, neither does {Binding Self}(where the latter is a shortcut for the former).

{Binding Path=Self}不起作用,也不起作用{Binding Self}(后者是前者的捷径)。

回答by Heinzi

Short answer:{Binding}is nota shortcut for "binding to itself" (in the sense of RelativeSource.Self). Rather, {Binding}is equivalent to{Binding Path=.}, which binds to the current source.

简短的回答{Binding}不是为“结合自身”(在意义上的快捷方式RelativeSource.Self)。相反,{Binding}相当于{Binding Path=.},它绑定到当前源。



To elaborate: A binding has a sourceand a path. You can do a "binding to itself", for example, by using

详细说明:绑定具有路径。您可以执行“绑定到自身”,例如,通过使用

<myUIControl myProperty="{Binding RelativeSource={RelativeSource Self}, Path=x}" />

This, however, sets the sourceto the control itself, so it will try to access property xof the UI control (rather than property xof the current data context). From how I understood your question, this is not what you want; in particular, it is not what {Binding}does: {Binding}keeps the source as it is (usually the DataContextof some parent element) and binds to the source itself (equivalent to Path=.).

但是,这会将设置为控件本身,因此它将尝试访问xUI 控件的属性(而不是x当前数据上下文的属性)。从我对你的问题的理解来看,这不是你想要的;特别是,它不是什么{Binding}{Binding}保持源的原样(通常DataContext是某个父元素的 )并绑定到源本身(相当于Path=.)。