As I understand it...
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
flows data into the UI,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
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.
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.
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.
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.
DataContext
is a more general feature in WPF, as suggested by its ownership by the low-level FrameworkElement class.
Content
is much more specific:
ContentControl
-- other controls such as ListBox
do not own a Content property themselves, but use a ContentControl somewhere in their control templates).ContentTemplate
-- that is, Content is what to display, and ContentTemplate is how to display it. (Button
is a good example of this.)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With