简体   繁体   English

wpf 数据上下文中的 {binding} 和继承有什么不同?

[英]What is different between {binding} and inherit in wpf datacontext?

I have some problems about datacontext binding.我有一些关于数据上下文绑定的问题。

My app has virtualization listbox.我的应用程序有虚拟化列表框。

Sometimes Button is not fired dataContextChanged.有时 Button 不会触发 dataContextChanged。

So I found this.所以我找到了这个。

<Grid DataContext={Binding ~~>
  <Button DataContext={Binding}/>
 </Grid>
 <Grid DataContext={Binding ~~>
  <Button/>
 </Grid>

My Code is first one.我的代码是第一个。 but It's not fired DataContextChanged sometimes, So I changed code to second one.但有时它不会触发 DataContextChanged,所以我将代码更改为第二个。

snoop said first one is from inherit, and second one is from parentTemplate. snoop 说第一个来自继承,第二个来自 parentTemplate。

What is different first one and second one?第一个和第二个有什么不同?

TL;DR: TL;博士:

SomeProperty={Binding} will bind the SomeProperty to the ENTIRE DataContext object from the parent. SomeProperty={Binding} 会将 SomeProperty 绑定到父级的整个 DataContext object。 SomeProperty does not have to be the DataContext from the child. SomeProperty 不必是子级的 DataContext。 In this special case I don't think there is any difference, since the DataContext is inherited anyways.在这种特殊情况下,我认为没有任何区别,因为 DataContext 无论如何都是继承的。 You are simply explicitly stating, what is already the default.您只是明确说明,什么已经是默认设置。 Discussed here 在这里讨论

More Info:更多信息:

Here is an explanation from the official documentation for这是官方文档的解释

 <Button DataContext={Binding}/>

As long as the binding already has a data context (for example, the inherited data context coming from a parent element), and whatever item or collection being returned by that context is appropriate for binding without requiring further path modification, a binding declaration can have no clauses at all: {Binding}.只要绑定已经有一个数据上下文(例如,来自父元素的继承数据上下文),并且该上下文返回的任何项或集合都适合绑定而不需要进一步修改路径,绑定声明就可以具有根本没有子句:{Binding}。 This is often the way a binding is specified for data styling, where the binding acts upon a collection.这通常是为数据样式指定绑定的方式,其中绑定作用于集合。 For more information, see Using Entire Objects as a Binding Source .有关详细信息,请参阅使用整个对象作为绑定源

and from here这里开始

<ListBox ItemsSource="{Binding}"
         IsSynchronizedWithCurrentItem="true"/>

The above example uses the empty binding syntax: {Binding}.上面的示例使用空绑定语法:{Binding}。 In this case, the ListBox inherits the DataContext from a parent DockPanel element (not shown in this example).在这种情况下,ListBox 从父 DockPanel 元素(未在此示例中显示)继承 DataContext。 When the path is not specified, the default is to bind to the entire object.不指定路径时,默认绑定到整个object。 In other words, in this example, the path has been left out because we are binding the ItemsSource property to the entire object.换句话说,在此示例中,路径已被省略,因为我们将 ItemsSource 属性绑定到整个 object。 (See the Binding to collections section for an in-depth discussion.) (有关深入讨论,请参阅绑定到 collections 部分。)

In general, the DataContext is inherited from parent elements.通常,DataContext 继承自父元素。 So in your 2nd example the button gets the DataContext from the Grid.因此,在您的第二个示例中,按钮从网格中获取 DataContext。 Example 例子

"ItemsControl" are special: Example “ItemsControl”很特殊: 示例

I don't know what ~~ is, is this meant to be a placeholder?我不知道~~是什么,这是要占位符吗?

Some more information on Bindings and Markup Extensions: Link1 Link2 Link3有关绑定和标记扩展的更多信息: Link1 Link2 Link3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM