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:
- ContentTemplate attribute describes how the items are desplayed.
- WorkspacesTemplate sets ItemsSource’s attribute to {Binding} meaning it’s bound to its DataContext property (DataContext of HeaderedContentControl)
- So I look at HeaderedContentControl’s dataContext, but it is described as “{Binding}” as well…
What does that mean?
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 itsDataContextproperty set to the equivalent of{Binding}.Remember:
Property="{Binding}"means “setthis.Propertyto the evaluated value ofthis.DataContext“Property="{Binding Path=SubProperty}"means “setthis.Propertyto the evaluated value ofthis.DataContext.SubProperty“This means that
DataContext="{Binding}"means “setthis.DataContextto the evaluated value ofthis.DataContext“, which (in most cases) is redundant!