Reputation: 35318
I'm learning WPF and currently studying this MSDN resource. At the moment, I'm focusing on a particular line in a data template from the supplied downloadable code:
<DataTemplate x:Key="WorkspacesTemplate">
<TabControl
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource ClosableTabItemTemplate}"
Margin="4"
/>
</DataTemplate>
I understand, e.g. from resources such as this SO post, that ItemsSource="{Binding}"
will bind to the associated DataContext
, but whereas the example in the latter SO post shows a DataContext
:
<ListView
DataContext="{StaticResource XMLFileGroups}"
ItemContainerStyle="{StaticResource XMLItemStyle}"
ItemsSource="{Binding}">
I cannot find one in my example above. Can someone please help me see what I'm missing here?
>>UPDATE:
Based on comments, I understand now that the data context comes from the container that uses the DataTemplate
, so here is the XAML from MainWindow
where the template it being used. Note that I still can't find a DataContext
anywhere:
<Window
x:Class="DemoApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:DemoApp.ViewModel"
FontSize="13"
FontFamily="Verdana"
MinWidth="650" MinHeight="420"
Title="{Binding Path=DisplayName}"
Width="650" Height="420"
WindowStartupLocation="CenterScreen"
>
<Window.Resources>
<ResourceDictionary Source="MainWindowResources.xaml" />
</Window.Resources>
<DockPanel>
<DockPanel DockPanel.Dock="Top" KeyboardNavigation.TabNavigation="None">
<Menu KeyboardNavigation.TabNavigation="Cycle">
<MenuItem Header="_File">
<MenuItem Header="E_xit" Command="{Binding Path=CloseCommand}" />
</MenuItem>
<MenuItem Header="_Edit" />
<MenuItem Header="_Options" />
<MenuItem Header="_Help" />
</Menu>
</DockPanel>
<Grid Margin="4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="4" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border
Grid.Column="0"
Style="{StaticResource MainBorderStyle}"
Width="170"
>
<HeaderedContentControl
Content="{Binding Path=Commands}"
ContentTemplate="{StaticResource CommandsTemplate}"
Header="Control Panel"
Style="{StaticResource MainHCCStyle}"
/>
</Border>
<Border
Grid.Column="2"
Style="{StaticResource MainBorderStyle}"
>
<HeaderedContentControl
Content="{Binding Path=Workspaces}"
ContentTemplate="{StaticResource WorkspacesTemplate}"
Header="Workspaces"
Style="{StaticResource MainHCCStyle}"
/>
</Border>
</Grid>
</DockPanel>
</Window>
FYI, the WorkspacesTemplate
is being used 8 lines from the bottom in case you can't see it.
Upvotes: 0
Views: 270
Reputation: 4116
The data context in first case is context provided by parent control to which this Template is applied.
So wherever you use this data template, that control will set the context.
Upvotes: 1