I have a simple Product and UserControl class called ProductSummaryControl. ProductSummaryControl displays the details for the Product class, which is passed to its DataContext. I checked that this works when I configure the control and its property manually.
I run into a problem when I try to use ProductSummaryControl as part of an ItemTemplate ListBox. This is the corresponding code:
<ListBox x:Name="ProductsList" HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<local:ProductSummaryControl DataContext="{Binding}" HorizontalAlignment="Stretch"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I set the ListBox ItemsSource to the list and I see several instances of my ProductSummaryControl that match the number of products in the list, but the controls are not properly attached to the data. All the examples I found in setting up an ItemTemplate on the Internet suggest that {Binding} is the right way to get the value of an item, but I obviously missed something ...
UPDATE:
I did a bit more research, and I found that the DataContext for ProductSummaryControl is set to the default instance of Product (). I think this is because of this:
<UserControl.Resources>
<DBSchmid_Data:Product x:Key="ProductDataSource" d:IsDataSource="True"/>
</UserControl.Resources>
<UserControl.DataContext>
<Binding Mode="OneWay" Source="{StaticResource ProductDataSource}"/>
</UserControl.DataContext>
DataConttext UserConttext Blend, DataContext. , DataContext , , . -, -, , .