winforms – 在Windows Phone中实现拖动和交换项目

我是
Windows手机的新手.我的问题如下:

我有一个按钮的项目网格.我想为按钮实现拖动和交换功能.如何在WP7平台上执行此操作. 最佳答案 这是解决方案! xaml看起来像这样

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
                <toolkit:WrapPanel Height="510" HorizontalAlignment="Left" Margin="18,56,0,0" Name="wrapPanel1" VerticalAlignment="Top" Width="411">
                    <Button Content="1" Height="124" Name="button2" Width="134">
                        <toolkit:GestureService.GestureListener>
                            <toolkit:GestureListener DragDelta="GestureListener_DragDelta" DragStarted="GestureListener_DragStarted" DragCompleted="GestureListener_DragCompleted" />
                        </toolkit:GestureService.GestureListener>
                    </Button>
                    <Button Content="2" Height="124" Name="button3" Width="134" >
                        <toolkit:GestureService.GestureListener>
                            <toolkit:GestureListener DragDelta="GestureListener_DragDelta" DragStarted="GestureListener_DragStarted" DragCompleted="GestureListener_DragCompleted" />
                        </toolkit:GestureService.GestureListener>
                    </Button>
                    <Button Content="3" Height="124" Name="button4" Width="134">
                        <toolkit:GestureService.GestureListener>
                            <toolkit:GestureListener DragDelta="GestureListener_DragDelta" DragStarted="GestureListener_DragStarted" DragCompleted="GestureListener_DragCompleted" />
                        </toolkit:GestureService.GestureListener>
                    </Button>
                    <Button Content="4" Height="124" Name="button5"  Width="134">
                        <toolkit:GestureService.GestureListener>
                            <toolkit:GestureListener DragDelta="GestureListener_DragDelta" DragStarted="GestureListener_DragStarted" DragCompleted="GestureListener_DragCompleted" />
                        </toolkit:GestureService.GestureListener>
                    </Button>
                    <Button Content="5" Height="124" Name="button6"  Width="134">
                        <toolkit:GestureService.GestureListener>
                            <toolkit:GestureListener DragDelta="GestureListener_DragDelta" DragStarted="GestureListener_DragStarted" DragCompleted="GestureListener_DragCompleted" />
                        </toolkit:GestureService.GestureListener>
                    </Button>
                    <Button Content="6" Height="124" Name="button7"  Width="134">
                        <toolkit:GestureService.GestureListener>
                            <toolkit:GestureListener DragDelta="GestureListener_DragDelta" DragStarted="GestureListener_DragStarted" DragCompleted="GestureListener_DragCompleted" />
                        </toolkit:GestureService.GestureListener>
                    </Button>
                    <Button Content="7" Height="124" Name="button8"  Width="134">
                        <toolkit:GestureService.GestureListener>
                            <toolkit:GestureListener DragDelta="GestureListener_DragDelta" DragStarted="GestureListener_DragStarted" DragCompleted="GestureListener_DragCompleted" />
                        </toolkit:GestureService.GestureListener>
                    </Button>
                    <Button Content="8" Height="124" Name="button9"  Width="134">
                        <toolkit:GestureService.GestureListener>
                            <toolkit:GestureListener DragDelta="GestureListener_DragDelta" DragStarted="GestureListener_DragStarted" DragCompleted="GestureListener_DragCompleted" />
                        </toolkit:GestureService.GestureListener>
                    </Button>
                    <Button Content="9" Height="124" Name="button10"  Width="134">
                        <toolkit:GestureService.GestureListener>
                            <toolkit:GestureListener DragDelta="GestureListener_DragDelta" DragStarted="GestureListener_DragStarted" DragCompleted="GestureListener_DragCompleted" />
                        </toolkit:GestureService.GestureListener>
                    </Button>
                    <Button Content="10" Height="124" Name="button11" MouseLeftButtonDown="mouseDown" Width="134">
                        <toolkit:GestureService.GestureListener>
                            <toolkit:GestureListener DragDelta="GestureListener_DragDelta" DragStarted="GestureListener_DragStarted" DragCompleted="GestureListener_DragCompleted" />
                        </toolkit:GestureService.GestureListener>
                    </Button>
                    <Button Content="11" Height="124" Name="button12"  Width="134">
                        <toolkit:GestureService.GestureListener>
                            <toolkit:GestureListener DragDelta="GestureListener_DragDelta" DragStarted="GestureListener_DragStarted" DragCompleted="GestureListener_DragCompleted" />
                        </toolkit:GestureService.GestureListener>
                    </Button>
                    <Button Content="12" Height="124" Name="button13" Width="134">
                        <toolkit:GestureService.GestureListener>
                            <toolkit:GestureListener DragDelta="GestureListener_DragDelta" DragStarted="GestureListener_DragStarted" DragCompleted="GestureListener_DragCompleted" />
                        </toolkit:GestureService.GestureListener>
                    </Button>
                </toolkit:WrapPanel>
            </Grid>

现在C#背后的代码

TranslateTransform translateTransform;

PointCollection points = new System.Windows.Media.PointCollection();

Button firstObject;

 private void GestureListener_DragStarted(object sender, DragStartedGestureEventArgs e)
     {
             translateTransform = new TranslateTransform();
             firstObject = sender as Button;
             var transform1 = firstObject.TransformToVisual(wrapPanel1);
            Point absolutePosition1 = transform1.Transform(new Point(0, 0));                 
     }







private void GestureListener_DragDelta(object sender, DragDeltaGestureEventArgs e)
      {
          var moveablebutton = sender as Button;
          moveablebutton.RenderTransform = translateTransform;
           translateTransform.X += e.HorizontalChange;
           translateTransform.Y += e.VerticalChange;          
      }





 private void GestureListener_DragCompleted(object sender, DragCompletedGestureEventArgs e)
   {
                var lastObject = sender as Button;
                Button b=new Button();
                int count = wrapPanel1.Children.Count;
                int index = wrapPanel1.Children.IndexOf(lastObject);
                var transform2 = lastObject.TransformToVisual(wrapPanel1);
                Point absolutePositon2 = transform2.Transform(new Point(0, 0));
                Point P1;
                Point P2 ;
                Point P3 ;
                Point P4 ;
                P1 = e.GetPosition(wrapPanel1);
                P2 = new Point(P1.X+134, P1.Y+124);



                Button swapitem=null;

                foreach (UIElement ctrl in wrapPanel1.Children)
                {
                    int index2;
                    var transform3 = ctrl.TransformToVisual(wrapPanel1);
                    Point comparePos = transform3.Transform(new Point(0, 0));

                    swapitem = ctrl as Button;
                    index2 = wrapPanel1.Children.IndexOf(swapitem);
                    if (index != index2)
                    {
                        P3 = new Point(comparePos.X, comparePos.Y);
                        P4 = new Point(comparePos.X + 158, comparePos.Y+170);
                       if (!(P2.Y < P3.Y || P1.Y > P4.Y || P2.X < P3.X || P1.X > P4.X))
                        {
                            swapitem = ctrl as Button;
                            index2 = wrapPanel1.Children.IndexOf(swapitem);
                            b = lastObject as Button;
                            b.RenderTransform = translateTransform;
                            translateTransform.X -= e.HorizontalChange;
                            translateTransform.Y -= e.VerticalChange;
                            wrapPanel1.Children.Remove(lastObject);
                            wrapPanel1.Children.Remove(swapitem);
                            // wrapPanel1.Children.RemoveAt(index2);
                            if (index < index2)
                            {
                                wrapPanel1.Children.Insert(index, swapitem);
                                wrapPanel1.Children.Insert(index2, b);
                            }
                            else
                            {
                                wrapPanel1.Children.Insert(index2, b);
                                wrapPanel1.Children.Insert(index, swapitem);
                            }
                            break;
                        }}

                }

            }
点赞