WPF 绑定语法问题
我已经看到这个语法出现了,并尝试用谷歌搜索它的定义,但没有成功;当 dp 以这种方式绑定时意味着什么?
<Grid>
<ContentControl Content="{Binding}"/>
</Grid>
我假设您必须绑定到 DataContext 或另一个元素上的某些属性,但这似乎没有绑定任何内容。
I've seen this syntax show up, and have tried to google for it's definition to no avail; what does it mean when a dp is bound this way?
<Grid>
<ContentControl Content="{Binding}"/>
</Grid>
I was under the assumption that you have to bind to some property on the DataContext, or another element, but this appears to bind to nothing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信这意味着您正在绑定到任何绑定上下文的根。因此,如果您在属于某种列表控件的数据模板中使用此语法,则您将绑定到父控件(列表控件)所绑定到的任何内容的根级别。
I believe it means you are binding to the root of whatever the binding context is. So if you use this syntax in a datatemplate that is part of some sort of list control, you would be binding to the root level of whatever the parent control (the list control) was binding to.
我相信 {Binding} 指的是 DataContext 本身。
编辑(澄清):我所说的DataContext是指当前级别的DataContext。例如,如果窗口的 DataContext 绑定到 List,则将窗口中 ListBox 控件上的 ItemsSource 设置为 {Binding} 会将 ListBox 绑定到 List 本身,而不是 List 的属性(如 Count)。
I believe {Binding} refers to the DataContext itself.
edit (clarification): By DataContext I mean the current level DataContext. For example, if your window's DataContext is bound to a List, then setting ItemsSource on a ListBox control in your window to {Binding} would bind the ListBox to the List itself, not a property of the List, like Count.
{Binding} 用于 {Binding [CurrentDataContext]}
{Binding} is for {Binding [CurrentDataContext]}
{Binding} 意味着您想要绑定到当前的 DataContext,该 DataContext 可以在对象本身上设置。如果当前对象没有设置 DataContext,那么它将沿着 VisualTree 向上查找具有 DataContext 的最近的父级。
{Binding} means that you want to Bind to the the current DataContext which could be set on the object itself. If no DataContext is set on the current object, then it will walk up the VisualTree and find the closest Parent that has a DataContext.