I'm trying to find out where the items in a HeaderedContentControl come from in a project that's not mine. Here's the code:
<HeaderedContentControl Content="{Binding Path=Workspaces}" ContentTemplate="{StaticResource WorkspacesTemplate}" Header="Workspaces" Style="{StaticResource MainHCCStyle}" DataContext="{Binding}" // <--- this /> <DataTemplate x:Key="WorkspacesTemplate"> <TabControl IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" ItemTemplate="{StaticResource ClosableTabItemTemplate}" Margin="4" />
so let's examine it:
What does that mean?
Data context is the network of connections among data points. Those connections may be created as metadata or simply identified and correlated. Contextual metadata adds value, essentially making it possible to receive information from data. A single data point on its own is useless.
DataContext does not generate template, it only used to hold common data for other controls to bind. In terms of ItemsSource property, it is mainly used to generate template regardless of you set it in XAML or in the code behind. DataContext is mainly used to hold common data that other child want to share.
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 data binding? Data binding is the process that establishes a connection between the app UI and the data it displays. If the binding has the correct settings and the data provides the proper notifications, when the data changes its value, the elements that are bound to the data reflect changes automatically.
Without seeing more of your code it is hard to be certain, but DataContext="{Binding}"
is quite often unnecessary, as any object in the current binding context will automatically have its DataContext
property set to the equivalent of {Binding}
.
Remember:
Property="{Binding}"
means "set this.Property
to the evaluated value of this.DataContext
"Property="{Binding Path=SubProperty}"
means "set this.Property
to the evaluated value of this.DataContext.SubProperty
"This means that DataContext="{Binding}"
means "set this.DataContext
to the evaluated value of this.DataContext
", which (in most cases) is redundant!
{Binding}
is something like bind 'this' or current data context - assigned or inherited from parents. For better understanding, equivalent for {Binding}
is {Binding .}
or {Binding Path=.}
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