Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3433620
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:35:54+00:00 2026-05-18T07:35:54+00:00

I have a few UserControls in my application which I need to support dragging

  • 0

I have a few UserControls in my application which I need to support dragging and dropping from, so I extracted the code into an abstract class extending UserControl (code below). When I use this in one control which is part of a DataTemplate in a ListBox, everything works fine.

When I use it in the control which can also be the drop target, I get the following exception on the DoDragDrop line:

COMException
Error HRESULT E_FAIL has been returned from a call to a COM component

This seems possibly related to WinForms interop, but I’m not using any WinForms or COM components – the application is pure WPF.

If I just continue execution, the drop happened successfully. If I surround the DoDragDrop call with a try block with empty catch block, everything seems to work as expected. I really don’t want to ship code with this sort of a hack though.

public abstract class DraggableUserControl : UserControl
{
    private Point? lastMouseDownPoint;

    protected override void  OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        base.OnMouseLeftButtonDown(e);

        lastMouseDownPoint = e.GetPosition(this);
    }

    protected override void OnMouseMove(System.Windows.Input.MouseEventArgs e)
    {
        base.OnMouseMove(e);

        if (e.LeftButton == MouseButtonState.Pressed && lastMouseDownPoint != null)
        {
            Point mousePosition = e.GetPosition(this);

            if (((Point)lastMouseDownPoint - mousePosition).Length > 3)
            {
                BeginDrag();
            }
        }
    }

    protected override void OnMouseLeave(MouseEventArgs e)
    {
        base.OnMouseLeave(e);

        if (e.LeftButton == MouseButtonState.Pressed && lastMouseDownPoint != null)
        {
            BeginDrag();
        }
    }

    protected override void OnMouseUp(MouseButtonEventArgs e)
    {
        base.OnMouseUp(e);

        if (e.ChangedButton == MouseButton.Left)
        {
            lastMouseDownPoint = null;
        }
    }

    private void BeginDrag()
    {
        DataObject dragData = new DataObject(DragFormat, DragData);

        //try
        //{
            DragDrop.DoDragDrop(this, dragData, DragDropEffects.Move);
        //} catch {}

        lastMouseDownPoint = null;
    }

    protected abstract String DragFormat
    { get; }

    protected abstract Object DragData
    { get; }

    protected abstract DragDropEffects DragAllowedEffects
    { get; }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T07:35:54+00:00Added an answer on May 18, 2026 at 7:35 am

    A simple example created with your class seems to work just fine. I used a string and DragFormat of DataFormats.StringFormat. It worked just fine.
    Hans is right, there is no way to repro.

    Im assuming whatever the Data object is , somehow mucks up the Get Data reflection or however it transfers it back.

    My suggestion would be to break up your data object and see if any particular part has the same issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my windows application i have a usercontrol, which in turn host few other
I have few columns in an excel sheet for which I need to do
I have few asynchronous tasks running and I need to wait until at least
I have few different applications among which I'd like to share a C# enum.
I have a few C# .dll projects which are common to many applications. Currently,
We have a few projects that involve building an application that is composed of
I have a few lines of PowerShell code that I would like to use
I have a WPF application that contains some Window s with few UserControl s
I have few DB tables, witch are build using inheritance from one table witch
I have few settings which I could place in a separate XML file and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.