How to drag and drop an item into a XAML application for Windows 8?

XAML / C # Windows 8 app ...

I used MouseDragElementBehavior in XAML / C # to move an item on the screen.

Unfortunately, the assembly of interactions does not work when developing an application for Windows 8.

How to drag an item in a XAML application for Windows 8?

Thanks.

EDIT: I found an example here: http://code.msdn.microsoft.com/windowsapps/Input-3dff271b/sourcecode?fileId=44758&pathId=962809525

Just copy the code and I can drag the item. If you encounter some problems, update if you need help.

+4
source share
2 answers

You need to handle the manipulation events on the item you want to drag. And also set ManipulationMode to a value other than None on the element.

  • Calling ManipulationStarted to Initialize Your Drag and Drop Code
  • Refer to ManipulationDelta , checking the e.Delta values ​​and shifting your element using RenderTransform , or if in Canvas , use the coordinates of the canvas.

Hope this helps.

+2
source

Below is a simplified example based on ColinE's answer.

Consider an ellipse canvas:

 <Canvas Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Ellipse Fill="Red" Canvas.Left="100" Canvas.Top="100" Width="100" Height="100" ManipulationMode="All" ManipulationDelta="Ellipse_ManipulationDelta_1"/> </Canvas> 

Now in the code behind you are handling ManipulationDelta:

 private void Ellipse_ManipulationDelta_1(object sender, ManipulationDeltaRoutedEventArgs e) { Ellipse myEllipse = (Ellipse)sender; Canvas.SetLeft(myEllipse, Canvas.GetLeft(myEllipse) + e.Delta.Translation.X); Canvas.SetTop(myEllipse, Canvas.GetTop(myEllipse) + e.Delta.Translation.Y); } 
+1
source

Source: https://habr.com/ru/post/1436738/


All Articles