Search code examples
.netxamllistboxmicrosoft-metrodatatemplate

Metro App Access DataTemplate Control


I have a ListBox control with the following layout, that has a custom control in it:

<ListBox x:Name="testList" ItemsSource="{Binding}" BorderBrush="{x:Null}" BorderThickness="0" Margin="0" Padding="0">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <controls:MyUserControl x:Name="testListItem"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

I am trying to access MyUserControl from codebehind like so:

testList.ItemContainerGenerator.ContainerFromItem(testList.SelectedItem);

Unfortunately, it always returns null. I've read somewhere that setting VirtualizingStackPanel.IsVirtualized="False" on the ListBox in XAML fixes this.

But this property is not available in Metro. Does someone know of a way on how this could be achieved?


Solution

  • RESOLVED Followed this approach:

    Reference Datatemplate Solution

    Works well. Enjoy!