Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Drag and Drop in UWP

$
0
0

Let's say we have a set of positions and we have to drag filled position and drop into empty position.
In UWP render the custom control and

  1. add private field

    private DragDropGestureHandler dragAndDropHandler = new DragDropGestureHandler();
    
  2. in OnElementPropertyChanged function

    protected override void OnElementChanged(ElementChangedEventArgs<PositionCustomView> e)
    {
    
        base.OnElementChanged(e);
    
        if (e.OldElement == null)
        {
            this.CanDrag = true;
            this.AllowDrop = true;
            this.DragOver += dragAndDropHandler.PositionCustomViewRenderer_DragOver;
            this.DragStarting += dragAndDropHandler.PositionCustomViewRenderer_DragStarting;
            this.Drop += dragAndDropHandler.PositionCustomViewRenderer_Drop;
        }
    
        if (e.NewElement == null)
        {
            this.CanDrag = false;
            this.AllowDrop = false;
            this.DragOver -= dragAndDropHandler.PositionCustomViewRenderer_DragOver;
            this.DragStarting -= dragAndDropHandler.PositionCustomViewRenderer_DragStarting;
            this.Drop -= dragAndDropHandler.PositionCustomViewRenderer_Drop;
        }
    }
    
  3. DragDropGestureHandler

    public class DragDropGestureHandler
    
    {        
        public void PositionCustomViewRenderer_DragStarting(UIElement sender, DragStartingEventArgs args)
        {
            var viewRenderer = sender as PositionCustomViewRenderer;
            var element = viewRenderer?.Element;
            if (element.AllowMove)
            {
                args.Cancel = false;
                args.Data.Properties.Add("customView", (element));
                element.PositionMovingCommand?.Execute(element.Position);
            }
            else
            {
                args.Cancel = true;
            }
        }
    
    public async void PositionCustomViewRenderer_Drop(object sender, DragEventArgs e)
    {
        var viewRenderer = sender as PositionCustomViewRenderer;
        var element = viewRenderer?.Element;
        var result = e.DataView?.Properties?.
                    SingleOrDefault(p => p.Key == "customView" && p.Value.GetType() == typeof(PositionCustomView));
        var positionCustomView = result?.Value as PositionCustomView;
    
        element.PositionMovedCommand?.Execute(new Tuple<int, int?, int>(PositionCustomView.Position, PositionCustomView.SessionId, element.Position));
    }        
    
    public void PositionCustomViewRenderer_DragOver(object sender, DragEventArgs e)
    {
        var viewRenderer = sender as PositionCustomViewRenderer;
        var element = viewRenderer?.Element;
        if (element.AllowDrop)
        {
            e.AcceptedOperation = DataPackageOperation.Copy | DataPackageOperation.Move;
        }
        else
        {
            e.AcceptedOperation = DataPackageOperation.None;
        }
    }
    
    }
    

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>