Question

I've created a simple ItemsControl with an ItemTemplate and DataTemplate.

I assign a populated IEnumerable to the DataContext, but no Items show up.

This is my ItemsControl (XAML):

<ItemsControl Name="pnlMenu" DataContext="{Binding}"
    Grid.Row="0" Margin="5,17,0,28">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Vertical"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button BorderBrush="{x:Null}" BorderThickness="0" Background="DodgerBlue" Height="25" Width="185" Margin="5,0" RenderTransformOrigin="0.5,0.5">
                <Button.ContentTemplate>
                    <DataTemplate>
                        <StackPanel HorizontalAlignment="Left" Orientation="Horizontal" Width="170">
                            <Image Source="Assets/Square71x71Logo.scale-100.png" Width="35" />
                            <TextBlock Text="{Binding Title}" VerticalAlignment="Center" Margin="5,0,0,0" />
                        </StackPanel>
                    </DataTemplate>
                </Button.ContentTemplate>
            </Button>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

And this is code behind (C#):

public void SetMenuDataContent(IEnumerable<NavDataGroup> groups)
{
    pnlMenu.DataContext = groups;
}

This may sound strange, but I actually came from WP8.1 over to WP8 instead of the other way around.

This used to work in WP8.1, but why doesn't this work in WP8? MSDN showed me similar examples...

Here's an example of a group in the IEnumerable:

{
  "Groups":[
  {
    "UniqueId": "mainpage",
    "Title": "Start",
    "Subtitle": "U bent hier thuis",
    "ImagePath": "Assets/DarkGray.png",
    "Description": "Een snel overzicht...",
    "Items":
    [
      {
        "UniqueId": "outdoors",
        "Title": "Buitenbad",
        "Subtitle": "Het buitenbad is vandaag",
        "ImagePath": "Assets/Open.png",
        "Description" : "",
        "Content" : "Open"
      },
      {
        "UniqueId": "indoors",
        "Title": "Binnenbad",
        "Subtitle": "Het binnenbad is vandaag",
        "ImagePath": "Assets/Open.png",
        "Description" : "",
        "Content" : "Open"
      },
      {
        "UniqueId": "photos",
        "Title": "Foto's",
        "Subtitle": "",
        "ImagePath": "Assets/LightGray.png",
        "Description" : "",
        "Content" : "[\"Assets/LightGray.png\", \"Assets/DarkGray\", \"Assets/MediumGray\"]"
      }
    ]
  }
  ]
}
Was it helpful?

Solution

Here's a solution that works for me:

public async void SetMenuDataContent()
{
    this.DataContext = groups;
}

I'm binding directly to DataContext of the UserControl. The problem occurred when I tried to bind to a control inside the UserControl and the UserControl itself. Apparently an easy mistake to make.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top