WPF 绑定到自身
我有一个 WPF Window
,并且某处有一个 ListView
,我在其中绑定了 List
。
现在,在我的 ListView
中的某处有一个 TextBox
,并且 Content
属性设置为 {Binding}
。
但这是简写。如何编写完整的绑定来绑定到自身?
{Binding Path=Self}
不起作用,{Binding Self}
也不起作用(后者是前者的快捷方式)。
I've got a WPF Window
, and somewhere there is a ListView
where I bind a List<string>
to.
Now somewhere in my ListView
there is a TextBox
and the Content
property is set to {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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简短回答:
{Binding}
不是“绑定到自身”的快捷方式(在RelativeSource.Self)。相反,{Binding}
相当于{Binding Path=.}
,绑定到当前源。详细说明:绑定具有源和路径。您可以执行“绑定到自身”,例如,通过使用
This,但是,将源设置为控件本身,因此它将尝试访问属性
x
UI 控件(而不是当前数据上下文的属性x
)。从我对你的问题的理解来看,这不是你想要的;特别是,它不是{Binding}
所做的:{Binding}
保持源原样(通常是某些父元素的DataContext
)并绑定到源本身(相当于Path=.
)。Short answer:
{Binding}
is not a shortcut for "binding to itself" (in the sense of RelativeSource.Self). Rather,{Binding}
is equivalent to{Binding Path=.}
, which binds to the current source.To elaborate: A binding has a source and a path. You can do a "binding to itself", for example, by using
This, however, sets the source to the control itself, so it will try to access property
x
of the UI control (rather than propertyx
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 theDataContext
of some parent element) and binds to the source itself (equivalent toPath=.
).绑定到 self 的另一种方法:
空的
{}
绑定到relativeSource.Self。another way to bind to self :
an empty
{}
binds to the RelativeSource.Self.