WPF:内容和DataContext的特性之间的区别是什么?特性、区别、内容、WPF

2023-09-04 13:07:52 作者:頤汵

据我了解...

的DataContext 属性 在控件使用此属性作为数据源 是每个框架元件具有可用于流数据转换成屏幕的属性 在DataContext的有范围 的范围是根据其中的DataContext分配给对象树中建立了 如果你父元素上设置DataContext的(例如,一个窗口),该属性会向下流到所有子元素(如文本框) DataContext property "controls use this property as a data source" "is a property that each framework element has that can be used to flow data into the screen" "DataContext has scope" "the scope is established according to where the DataContext is assigned to in the object tree" "if you set the DataContext on a parent element (e.g. a Window), that property will flow down to all child elements (e.g. a text box)" 在该酒店发生在具体取决于所使用的控制许多名字: ContentControl.Content ItemsControl.ItemsSource Items.ItemsSource HeaderedContentControl.Header HeaderedContentControl.Content This property takes on many names depending on the control that is being used: ContentControl.Content ItemsControl.ItemsSource Items.ItemsSource HeaderedContentControl.Header HeaderedContentControl.Content

所以我的问题是:什么是内容之间的差异的DataContext 属性?这里有一个细微之处,我很想念。难道......

So my question is: what is the difference between the Content and DataContext properties? There is a nuance here that I am missing. Is it...

的DataContext 流动数据到用户界面, 这是内容属性来确定的工作(通常是扔了一个绑定)究竟会显示(通过内容presenter +的ContentTemplate) While the DataContext flows data into the UI, It is the job of the Content property to determine (usually threw a binding) what will be displayed (via ContentPresenter + ContentTemplate)

样品code

<Window x:Name="myWindow" DataContext="{Binding ClassA}> 
    <StackPanel> <!-- DataContext is set to ClassA -->

        <!-- DataContext is set to ClassA, ClassA.Name will be displayed -->
        <Label Content="{Binding Name}" />
    </StackPanel>
 </Window>

参考

MSDN: ContentControl.Content物业 MSDN: FrameworkElement.DataContext物业 在MSDN: WPF内容模型 值得一读 MSDN: ContentControl.Content Property MSDN: FrameworkElement.DataContext Property MSDN: WPF Content Model worth reading

推荐答案

的DataContext 是在WPF更一般的特征,所建议的所有权由低层次的FrameworkElement的类。

DataContext is a more general feature in WPF, as suggested by its ownership by the low-level FrameworkElement class.

在它参与绑定的所有的框架元素,作为默认绑定源。 正如你所说,一个元素的DataContext的被传递到子元素。 it participates in bindings for all framework elements, as the default binding source. as you mentioned, an element's DataContext is passed down to child elements.

内容是更具体的:

这是针对一组控制(主要是那些从 ContentControl中继承的控制非常有限的依赖属性 - 其他控件,如的ListBox 没有自己的内容属性本身,而是使用的在他们的控制模板某处ContentControl中)。 在它没有传下来像DataContext的,相反却是只关心用所拥有的控制和它的直接的关系(即绑定) 在使用它调用的东西,控制要显示,在控制自身不知道或不关心什么类型的对象,这将是。 在它经常被用来在cojunction与的ContentTemplate - 也就是说,内容是的什么的显示,并且的ContentTemplate是的如何,以显示它。 (按钮就是一个很好的例子。) it is a dependency property specific to a very limited set of controls (mostly those controls that inherit from ContentControl -- other controls such as ListBox do not own a Content property themselves, but use a ContentControl somewhere in their control templates). it is not passed down like the DataContext, but is rather concerned solely with the owning Control and its immediate relationships (ie, bindings) it is used by controls that call for something to be displayed, where the control itself does not know or care what type of object that will be. it is often used in cojunction with ContentTemplate -- that is, Content is what to display, and ContentTemplate is how to display it. (Button is a good example of this.)