I am still learning DataTemplate, but at this moment I would like just to underline (as a note to myself) that it seems that binding for DataTemplate goes to another class.
XAML:
<Window x:Class="ItemsControlItemTemplate.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:itemsControlItemTemplate="clr-namespace:ItemsControlItemTemplate"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<itemsControlItemTemplate:ItemsControlViewModel x:Key="ItemsControlViewModel" />
</Grid.Resources>
<ItemsControl DataContext="{StaticResource ItemsControlViewModel}" ItemsSource="{Binding myItemsSource}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<DataTemplate.Resources>
<itemsControlItemTemplate:AnotherModel x:Key="AnotherModel" />
</DataTemplate.Resources>
<Label DataContext="{StaticResource AnotherModel}" Content="{Binding myContent}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Window>
Here note part:
<DataTemplate.Resources>
<itemsControlItemTemplate:AnotherModel x:Key="AnotherModel" />
</DataTemplate.Resources>
<Label DataContext="{StaticResource AnotherModel}" Content="{Binding myContent}" />
My view model:
public class ItemsControlViewModel
{
public IEnumerable<string> myItemsSource { get; set; }
public ItemsControlViewModel()
{
myItemsSource = new[] { "Test1", "Test2" };
}
}
public class AnotherModel
{
public string myContent { get; set; }
public AnotherModel()
{
myContent = "test";
}
}
Here notice class:
public class AnotherModel
{
public string myContent { get; set; }
public AnotherModel()
{
myContent = "test";
}
}
Which is actually in use in the label under the datatemplate
Example project you can download from here.