Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: What is the difference between the Content and DataContext properties?

Tags:

c#

.net

wpf

As I understand it...

  • 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)"
  • Content property
    • This property takes on many names depending on the control that is being used:
      • ContentControl.Content
      • ItemsControl.ItemsSource
      • Items.ItemsSource
      • HeaderedContentControl.Header
      • HeaderedContentControl.Content

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

  1. While the DataContext flows data into the UI,
  2. It is the job of the Content property to determine (usually threw a binding) what will be displayed (via ContentPresenter + ContentTemplate)

SAMPLE 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>

REFERENCES

  • MSDN: ContentControl.Content Property
  • MSDN: FrameworkElement.DataContext Property
  • MSDN: WPF Content Model
    • worth reading
  • StackOverflow: What is DataContext for?
like image 845
Pressacco Avatar asked Feb 13 '14 22:02

Pressacco


People also ask

What's the difference between ContentControl and ContentPresenter in WPF?

ContentControl is a base class for controls that contain other elements and have a Content -property (for example, Button ). ContentPresenter is used inside control templates to display content.

What is the DataContext in WPF?

The DataContext property is the default source of your bindings, unless you specifically declare another source, like we did in the previous chapter with the ElementName property. It's defined on the FrameworkElement class, which most UI controls, including the WPF Window, inherits from.

What is content control in WPF?

Content Control is a base class that provides standardised functionality to WPF Controls. The Content Control class represents controls that can include a single item of content. This content is commonly plain text or a child control. Content Control is a subclass of the Control class in WPF.

What is the DataContext?

The DataContext is the source of all entities mapped over a database connection. It tracks changes that you made to all retrieved entities and maintains an "identity cache" that guarantees that entities retrieved more than one time are represented by using the same object instance.


1 Answers

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

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

Content is much more specific:

  • 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.)
like image 81
McGarnagle Avatar answered Sep 23 '22 10:09

McGarnagle