Search code examples
c#uwpdrag-and-dropuwp-xamluser-feedback

Custom Accepted Operation in Drag and Drop


I am making a library management software. This is the screenshot: screenshot

I have implementing functionalities like dragging a book into the delete icon to delete the book. But there are two obstacles:

  1. DataPackageOperation has only four possibilities: Copy, link, move none. So, after four, it is difficult to differentiate which AppBarButton is the book dropped on.
  2. I plan to add more items to the CommandBar. But there are only four possible operations

I need a way to give the user a custom feedback as to which AppBarButton is the book currently dragged over. DataPackageOperation contains only four. Out of which, 'None' cannot be used(because it will be confusing). Is there a way to provide that feedback?


Solution

  • I need a way to give the user a custom feedback as to which AppBarButton is the book currently dragged over

    You could give the user a custom feedback via custom Drag UI. The follow code comes from XamlDragAndDrop official code sample.

    private void TargetTextBox_DragEnter(object sender, Windows.UI.Xaml.DragEventArgs e)
    {
        /// Change the background of the target
        VisualStateManager.GoToState(this, "Inside", true);
        bool hasText = e.DataView.Contains(StandardDataFormats.Text);
        e.AcceptedOperation = hasText ? DataPackageOperation.Copy : DataPackageOperation.None;
        if (hasText)
        {
            e.DragUIOverride.Caption = "Drop here to insert text";
            // Now customize the content
            if ((bool)HideRB.IsChecked)
            {
                e.DragUIOverride.IsGlyphVisible = false;
                e.DragUIOverride.IsContentVisible = false;
            }
            else if ((bool)CustomRB.IsChecked)
            {
                var bitmap = new BitmapImage(new Uri("ms-appx:///Assets/dropcursor.png", UriKind.RelativeOrAbsolute));
                // Anchor will define how to position the image relative to the pointer
                Point anchor = new Point(0,52); // lower left corner of the image
                e.DragUIOverride.SetContentFromBitmapImage(bitmap, anchor);
                e.DragUIOverride.IsGlyphVisible = false;
                e.DragUIOverride.IsCaptionVisible = false;
            }
            // else keep the DragUI Content set by the source
        }
    }
    

    enter image description here