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
- This property takes on many names depending on the control that is being used:
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...
- 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)
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?