views:

50

answers:

2

I have the following user control that is embedded within another user control.

<UserControl.Resources>

    <DataTemplate x:Key="ContextsTemplate">
        <Label  Margin="10,0,20,0" Content="{Binding Name}"/>
    </DataTemplate>

</UserControl.Resources>

<ItemsControl Name="Contexts" 
                  Background="Transparent" 
                  ItemsSource="{Binding}" 
                  Margin="0,0,0,0"
                  VerticalAlignment="Center"
                  AlternationCount="2" 
                  ItemTemplate="{StaticResource ContextsTemplate}">
</ItemsControl>

Here is XAML code for the user control (controlB) above that is embedded in another user control (controlA).

<local:ContextContentsUserControl Height="30" Content="{Binding Contexts}"/>

controlA is displayed on the screen as "(Collection)" but for some reason doesn't show each item in the collections text in the label. Please help.

+2  A: 

When you declare your ContextContentsUserControl, you are setting its Content property. You need to be setting the DataContext instead:

<local:ContextContentsUserControl Height="30" DataContext="{Binding Contexts}" />
itowlson
+4  A: 

The problem is here:

Content="{Binding Contexts}"

You meant:

DataContext="{Binding Contexts}"

The reason you got "(Collection)" instead of the content you had defined for controlA is the content you defined in the XAML for ControlA was replaced with your collection. The body of a UserControl's XAML file simply sets its Content property: You replaced it after it was set.

Ray Burns