This question is locked. New answers and comments are not allowed.
I am building a DataTemplate and I expected setting the DataContext inside the cell template would work, but it doesn't. This is what I am trying:
<DataTemplate>
<StackPanel DataContext="{Binding Item}">
<TextBlock Text="{Binding Name}" />
<TextBlock Text="{Binding Price}" />
</StackPanel>
</DataTemplate>
If I change it to this it works but for a complex piece of code that's a lot of children objects:
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Item.Name}" />
<TextBlock Text="{Binding Item.Price}" />
</StackPanel>
</DataTemplate>
Shouldn't this work?
<DataTemplate>
<StackPanel DataContext="{Binding Item}">
<TextBlock Text="{Binding Name}" />
<TextBlock Text="{Binding Price}" />
</StackPanel>
</DataTemplate>
If I change it to this it works but for a complex piece of code that's a lot of children objects:
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Item.Name}" />
<TextBlock Text="{Binding Item.Price}" />
</StackPanel>
</DataTemplate>
Shouldn't this work?