2

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.

Zee
  • 381
  • 2
  • 11

1 Answers1

2

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!

Nate Diamond
  • 5,525
  • 2
  • 31
  • 57
  • Hey @Nate Diamond, Thanks for your answer, I checked it and seems like ContainerFromItem is only available in 8.1 but I am using windows 8. How we can do it in windows 8? – Zee Jun 13 '14 at 14:08
  • In windows 8 , there is ItemContainerGenerater that have this function, but it is not working and returning null ` var itemContainer = GVmain.ItemContainerGenerator.ContainerFromIndex(GVmain.SelectedIndex); Grid grid = itemContainer.GetFirstDescendantOfType(); ` itemContainer getting null – Zee Jun 13 '14 at 14:39
  • Ah, you're using Windows 8. Firstly, if you can, I would update to 8.1. Secondly, make sure that the item is actually in the list and that the list has been fully loaded first. If you do it before the list is initialized, it will always return `null`. When using `ContainerFromIndex`, does it return `null` or a `ListViewItem`, even if `GetFirstDescendantOfType` returns `null`. – Nate Diamond Jun 13 '14 at 16:28
  • Using ContainerFromIndex solves the problem, but still so not know why ContainerFromItem didn't work. – Zee Jun 14 '14 at 15:40
  • I know there have been quite a few reports of it having issues in the past, so I generally rely on `ContainerFromIndex`. – Nate Diamond Jun 14 '14 at 18:46