WPF Binding Syntax Question

我是研究僧i 提交于 2019-11-28 04:39:18

问题


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.


回答1:


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.




回答2:


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.




回答3:


{Binding} is for {Binding [CurrentDataContext]}




回答4:


{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.



来源:https://stackoverflow.com/questions/2502331/wpf-binding-syntax-question

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!