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
WPF Bind to itself
提问by Snake
I've got a WPF Window
, and somewhere there is a ListView
where I bind a List<string>
to.
我有一个 WPF Window
,在某个地方有一个ListView
我将 a 绑定List<string>
到的地方。
Now somewhere in my ListView
there is a TextBox
and the Content
property 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 x
of the UI control (rather than property x
of 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 DataContext
of some parent element) and binds to the source itself (equivalent to Path=.
).
但是,这会将源设置为控件本身,因此它将尝试访问x
UI 控件的属性(而不是x
当前数据上下文的属性)。从我对你的问题的理解来看,这不是你想要的;特别是,它不是什么{Binding}
:{Binding}
保持源的原样(通常DataContext
是某个父元素的 )并绑定到源本身(相当于Path=.
)。