简体   繁体   中英

Drag and drop inside C# ScrollViewer

I have found the following problem during the development of a metro app in c#: I define a draggable ellipse and add this as child of a canvas that is inside a scrollviewer. When i drag the ellipse without scrolling (in my case without orizontal scroll) there are no problem: the ellipse remain below the pointer. But when i scroll orizontally the scrollviewer and then try to move the draggable ellipse, this latter dont stay over the pointer. The following is the code that I developed so you can see the problem.

MainPage.xaml

<Page
    x:Class="draggableEllipse.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:draggableEllipse"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <ScrollViewer VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Visible" Canvas.Left="1325" Canvas.Top="12">
            <Canvas x:Name="canvas" HorizontalAlignment="Left" Height="800" VerticalAlignment="Top" Width="3311" Background="#FF60666A">
            </Canvas>
        </ScrollViewer>
    </Grid>
</Page>

MainPage.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// Il modello di elemento per la pagina vuota è documentato all'indirizzo http://go.microsoft.com/fwlink/?LinkId=234238

namespace draggableEllipse
{
    /// <summary>
    /// Pagina vuota che può essere utilizzata autonomamente oppure esplorata all'interno di un frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            draggableEllipse d = new draggableEllipse(300, 300, canvas);
        }
    }
}

draggableEllipse.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.Input;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;

namespace draggableEllipse
{
    class draggableEllipse
    {
        Ellipse e;

        public draggableEllipse(double x, double y,Canvas canvas)
        {
            e = new Ellipse();
            e.Width = 20;
            e.Height = 20;
            e.StrokeThickness = 2;
            e.Fill = new SolidColorBrush(Colors.White);
            e.Stroke = new SolidColorBrush(Colors.White);

            e.PointerPressed += Shape_PointerPressed;
            e.PointerMoved += Shape_PointerMoved;
            e.PointerReleased += Shape_PointerReleased;

            canvas.Children.Add(e);
            Canvas.SetLeft(e, x);
            Canvas.SetTop(e, y);

        }

        void Shape_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            Ellipse el = (Ellipse)sender;

            el.CapturePointer(e.Pointer);
        }

        void Shape_PointerMoved(object sender, PointerRoutedEventArgs e)
        {
            Ellipse el = (Ellipse)sender;

            // Only move the shape if one pointer is currently pressed
            if (el.PointerCaptures != null && el.PointerCaptures.Count == 1)
            {
                PointerPoint point = e.GetCurrentPoint(null);

                // Center the shape under the pointer
                Canvas.SetLeft(el, point.Position.X - (el.ActualWidth / 2));
                Canvas.SetTop(el, point.Position.Y - (el.ActualHeight / 2));

            }
        }

        void Shape_PointerReleased(object sender, PointerRoutedEventArgs e)
        {
            Ellipse el = (Ellipse)sender;

            el.ReleasePointerCapture(e.Pointer);
        }

    }
}

thank you in advance :D

I'd consider using manipulation events. I would set ManipulationMode="TranslateX,TranslateY,System" and in ManipulationStarted I would call CancelDirectManipulations(e) . I think pointer events don't work in a ScrollViewer when using touch. I think you might also need to set Vertical/HorizontalScrollMode on the SV.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM