Search code examples
c#gridviewwindows-8windows-store-appswinrt-xaml-toolkit

How I can get Gridview's SelectedItem's child Grid?


I have following structure :

     <GridView x:Name="GVmain" SelectionChanged="GVmain_SelectionChanged_1" ItemsSource="{Binding DateItemsView}" SelectionMode="None" Visibility="Visible" Padding="120,0,0,0" ScrollViewer.HorizontalScrollMode="Enabled" ScrollViewer.VerticalScrollMode="Disabled" Grid.Row="1"  ItemContainerStyle="{StaticResource GridViewItemStyleATLIST}">
            <!--<StackPanel Orientation="Horizontal">-->

            <GridView.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="0,0,30,0" Width="400" DataContext="{Binding}" >
                        <Grid.RowDefinitions>
                        ...

Using Xamwinrt toolkit, is there any way to select GVmain's selecteditem's Child Grid?

I think I can only cast SelectedItem to the Type of it's ItemSource.


Solution

  • Use ListView.ContainerFromItem, passing in ListView.SelectedItem. This will give you the ListViewItem of the SelectedItem. You can then use the normal GetDescendents or GetFirstDescendantOfType to parse the visual tree.

    i.e.

    // ContainerFromItem seems to have issues at times, so use ContainerFromIndex.
    //var itemContainer = GVmain.ContainerFromItem(GVmain.SelectedItem);
    var itemContainer = GVmain.ContainerFromIndex(GVmain.SelectedIndex);
    var rootGrid = itemContainer.GetFirstDescendantOfType<Grid>();
    

    Hope this helps and happy coding!