locked
getting item position in gridview

    Question

  • i'm trying to drag and drop elements from a gridview to another but also test on the margin of the draged element if it's in the right place or not, this is my code for drag and drop hope you will help me to define how to test on the position problem:

     <Page.Resources>
            <DataTemplate x:Key="ItemTemplate1">
                <Border Background="#25BDC0">
                    <Grid Width="230" Height="230" Margin="10">
                        <TextBlock Text="{Binding Title}" Style="{StaticResource HeaderTextStyle}"
                                   HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Grid>
                </Border>
            </DataTemplate>
    
            <DataTemplate x:Key="ItemTemplate2">
                <Border Background="#94C83C" >
                    <StackPanel Width="230" Height="230" Margin="10">
                        <TextBlock Text="{Binding Title}" Style="{StaticResource HeaderTextStyle}"
                                   TextAlignment="Center"/>
                        <TextBlock Text="{Binding Subtitle}" Style="{StaticResource SubtitleTextStyle}" 
                                   TextAlignment="Center" TextWrapping="Wrap" />
                    </StackPanel>
                </Border>
            </DataTemplate>
        </Page.Resources>
    
        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}" Width="Auto" Height="Auto">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
    
    <GridView Grid.Row="0"
                AllowDrop="True" CanDragItems="True" 
                DragItemsStarting="GridViewDragItemsStarting"
                Drop="GridViewDrop" Margin="10">
        <GridView.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </GridView.ItemsPanel>
               
            </GridView>
    
    <GridView Grid.Row="1" ItemTemplate="{StaticResource ItemTemplate2}"
                ItemsSource="{Binding SecondCollection}"
                AllowDrop="True" CanDragItems="True" 
                DragItemsStarting="GridViewDragItemsStarting"
                Drop="GridViewDrop" Margin="10">
        <GridView.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </GridView.ItemsPanel>
    </GridView>

    in the viewModel:

    public ObservableCollection<DemoItem> FirstCollection { get; set; }
    
            public ObservableCollection<DemoItem> SecondCollection { get; set; }
    
            public MainViewModel()
            {
                FirstCollection = new ObservableCollection<DemoItem>
                {
                    new DemoItem{ Title = "Item 1", Subtitle = "This is the same item as before..."},
                    new DemoItem{ Title = "Item 2", Subtitle = "It's just the not the same datatemplate"},
                    new DemoItem{ Title = "Item 3", Subtitle = "You can place this item wherever you want"},
                    new DemoItem{ Title = "Item 4", Subtitle = "You can reorder items in the same list"},
                };
    
                SecondCollection = new ObservableCollection<DemoItem>();
            }
    
            /// <summary>
            /// Switches the item.
            /// </summary>
            /// <param name="item">The item.</param>
            public void SwitchItem(DemoItem item)
            {
                if (FirstCollection.Contains(item))
                {
                    FirstCollection.Remove(item);
                    SecondCollection.Add(item);
                }
                else
                {
                    SecondCollection.Remove(item);
                    FirstCollection.Add(item);
                }
            }

    and in my code behind :

     private void GridViewDragItemsStarting(object sender, DragItemsStartingEventArgs e)
            {
                var item = e.Items.FirstOrDefault();
                if (item == null)
                    return;
    
                e.Data.Properties.Add("item", item);
                e.Data.Properties.Add("gridSource", sender);
            }
    
            private void GridViewDrop(object sender, DragEventArgs e)
            {
                object gridSource;
                e.Data.Properties.TryGetValue("gridSource", out gridSource);
    
                if (gridSource == sender)
                    return;
    
                object sourceItem;
                e.Data.Properties.TryGetValue("item", out sourceItem);
                if (sourceItem == null)
                    return;
    
                _mainViewModel.SwitchItem((DemoItem)sourceItem);
            }


    zipo

    Friday, March 14, 2014 9:07 AM

All replies

  • The DragEventArgs will return the drop point in the GetPosition method. You can use the VisualTreeHelper class to find what controls are at a given point.

    --Rob

    Saturday, March 15, 2014 12:38 AM
    Owner
  • can you help me with some code please, because my solution consists on a test on the draged element , i used GetPosition but every time i got a different value which is not a good approach for me to test on it ! any ideas or correct me if i'm doing something wrong 

    Thx


    zipo


    • Edited by Zied Nassr Monday, March 17, 2014 3:02 PM update
    Monday, March 17, 2014 2:22 PM