Search code examples
xamlmicrosoft-metrowindows-runtimeitemscontrol

WinRT XAML ItemsControl Children binding


I have the following XAML:

<ItemsControl ItemsSource="{Binding DataContext}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <local:MyControl DataContext="{Binding}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

How do I set the DataContext of MyControl to a single item of the ItemsControl?

NOTE: ItemsControl is embedded in a UserControl. The UserControl has it's DataContext property set in the place where it's being used.


Solution

  • Figured it out:

    <ItemsControl ItemsSource="{Binding}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <local:MyControl DataContext="{Binding}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
    

    Just changed {Binding DataContext} to {Binding}