locked
how to get index of the grid which is inside an gridview where we have dropping an element in metro app

    Question

  • how to get index of the grid which is inside an gridview where we have dropping an element in metro app
    Friday, September 7, 2012 8:59 AM

Answers

  • First, you should add DragItemStarting event in GridView

    <GridView x:Name="mygrid" Background="White" Margin="571,115,136,158" SelectionChanged="mygrid_SelectionChanged_1" CanDragItems="True" CanReorderItems="True" AllowDrop="True" 
                      DragItemsStarting="mygrid_DragItemsStarting_1">

    Then in that event, we can get the index.

    void animateitem::MainPage::mygrid_DragItemsStarting_1(Platform::Object^ sender, Windows::UI::Xaml::Controls::DragItemsStartingEventArgs^ e)
    {
    	GridView^ gridview=safe_cast<GridView^>(sender);
    	if(e->Items->Size)
    	{
    		auto container=gridview->ItemContainerGenerator->ContainerFromItem(e->Items->GetAt(0));
    		int index=gridview->ItemContainerGenerator->IndexFromContainer(container);
    	}
    
    }

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Jesse Jiang Monday, September 24, 2012 9:00 AM
    Thursday, September 13, 2012 4:34 AM

All replies

  • Can you please explain in more detail what you are looking for? It would help if you could provide minimal sample code to demonstrate what you are doing, repro steps to get there, and a clear explanation of the desired behavior and how it differs from what you actually get.

    --Rob

    Tuesday, September 11, 2012 12:03 AM
    Owner
  • hi rob,

    I am trying to find out the index of the gridview item where I am dropping an image .The xaml code is as follows(the drop methods works properly , but I can't find the index).Please help me..

    <GridView x:Name="contactsGridview_" ItemsSource="{Binding Source={StaticResource contactsCVS_}}" IsSwipeEnabled="False" ScrollViewer.IsHorizontalScrollChainingEnabled="True"

    Background="Transparent" ItemContainerStyle="{StaticResource gridViewItemStyle}" SelectionMode="Extended" AllowDrop="True" IsRightTapEnabled="True" PointerEntered="item_PointerEntered_1">

    <GridView.ItemTemplate>

    <DataTemplate x:Name="itemGrid_" >

    <Grid Height="70" Width="275" Background="Transparent" x:Name="item" AllowDrop="True" Drop="item_Drop_1" >

    <Grid.ColumnDefinitions>

    <ColumnDefinition Width="Auto"/>

    <ColumnDefinition Width="*"/>

    </Grid.ColumnDefinitions>

    <Image Source="Assets/contactpic.png" Stretch="Uniform" HorizontalAlignment="Left" VerticalAlignment="Center" Width="50"/>

    <StackPanel Margin="55,5,0,0">

    <TextBlock Text = "{Binding Name}" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontWeight="Bold" x:Name="t1"/>

    <TextBlock Text = "{Binding Email}" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Center" x:Name="t2"/>

    </StackPanel>

    </Grid>

    </DataTemplate>

    </GridView.ItemTemplate>

    </Gridview> iam tring to find out the index when I am dropping the item..

    Tuesday, September 11, 2012 3:49 AM
  • Hello,

    First, you should add
    CanDragItems="True" CanReorderItems="True" AllowDrop="True"
    In GridView so that the item can be dropped.

    Then, the Drop event would not handle the drop event. You should use itemsChangedEventHandler when user drop and change the item.
    Pleaser add these codes in your project.

    	mygrid->ItemContainerGenerator->ItemsChanged+=ref new Windows::UI::Xaml::Controls::Primitives::ItemsChangedEventHandler(this,&MainPage::ItemsChangedEventHandler);
    
    
    
    
    
    void animateitem::MainPage::ItemsChangedEventHandler(Object^ sender, ItemsChangedEventArgs^ e)
    {
    	GeneratorPosition gp=e->Position;
    	int i=gp.Index;
    	
    }

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, September 11, 2012 8:43 AM
  • hi jesse,

    in my case there is no item change only the dropping to a grid takes place . I am tried with the above code the item changed event is not invoking. Thank you for your valuable reply.

    I am trying to find the index when dropping an item to the grid in the gridview .Please help me..

    Regards,

    Sarath s

    Tuesday, September 11, 2012 12:58 PM
  • First, you should add DragItemStarting event in GridView

    <GridView x:Name="mygrid" Background="White" Margin="571,115,136,158" SelectionChanged="mygrid_SelectionChanged_1" CanDragItems="True" CanReorderItems="True" AllowDrop="True" 
                      DragItemsStarting="mygrid_DragItemsStarting_1">

    Then in that event, we can get the index.

    void animateitem::MainPage::mygrid_DragItemsStarting_1(Platform::Object^ sender, Windows::UI::Xaml::Controls::DragItemsStartingEventArgs^ e)
    {
    	GridView^ gridview=safe_cast<GridView^>(sender);
    	if(e->Items->Size)
    	{
    		auto container=gridview->ItemContainerGenerator->ContainerFromItem(e->Items->GetAt(0));
    		int index=gridview->ItemContainerGenerator->IndexFromContainer(container);
    	}
    
    }

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Jesse Jiang Monday, September 24, 2012 9:00 AM
    Thursday, September 13, 2012 4:34 AM