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

  • SEARCH
  • Home
  • 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 3487164
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:07:00+00:00 2026-05-18T11:07:00+00:00

For a simple test I want to drag a Button to a TextBox. I

  • 0

For a simple test I want to drag a Button to a TextBox. I can start dragging the Button, but the Drop event is not raised. What am I missing?

Xaml:

<Window x:Class="DayPlanner.View.DnDTest"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="DnDTest" Height="200" Width="200">
    <StackPanel>
        <Button Name="button" 
                Content="OK" 
                PreviewMouseLeftButtonDown="button_PreviewMouseLeftButtonDown" 
                PreviewMouseMove="button_PreviewMouseMove"/>
        <TextBox Name="textBox"
                 AllowDrop="True"
                 DragEnter="textBox_DragEnter"
                 Drop="textBox_Drop"/>
    </StackPanel>
</Window>

Code:

public partial class DnDTest : Window
{
    public DnDTest()
    {
        InitializeComponent();
    }

    private Point dragStartPoint;

    private void button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        dragStartPoint = e.GetPosition(null);
    }

    private static bool IsDragging(Point dragStartPoint, MouseEventArgs e)
    {
        var diff = e.GetPosition(null) - dragStartPoint;
        return
            e.LeftButton == MouseButtonState.Pressed &&
            (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
                Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance);
    }

    private void button_PreviewMouseMove(object sender, MouseEventArgs e)
    {
        if (IsDragging(dragStartPoint, e))
        {
            DragDrop.DoDragDrop(button, new DataObject("Button", button), DragDropEffects.Move);
            e.Handled = true;
        }
    }

    private void textBox_DragEnter(object sender, DragEventArgs e)
    {
        e.Handled = true;
    }

    private void textBox_Drop(object sender, DragEventArgs e)
    {
        var button = (Button)e.Data.GetData("Button");
        textBox.Text = string.Format("[0]", button.Content.ToString());
        e.Handled = true;
    }
}
  • 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-18T11:07:00+00:00Added an answer on May 18, 2026 at 11:07 am

    This might be some strange case, but to fix it, I needed to handle or dragging events, including the Preview versions.

    Here’s how to make it work.

    Xaml:

    <Window x:Class="DayPlanner.View.DnDTestBasic"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="DnDTestBasic" Height="200" Width="200">
        <StackPanel>
            <Button Name="button" 
                    Content="OK" 
                    PreviewMouseLeftButtonDown="button_PreviewMouseLeftButtonDown" 
                    PreviewMouseMove="button_PreviewMouseMove"/>
            <TextBox Name="textBox"
                     AllowDrop="True"
                     PreviewDragEnter="textBox_Dragging"
                     DragEnter="textBox_Dragging"
                     PreviewDragOver="textBox_Dragging"
                     DragOver="textBox_Dragging"
                     Drop="textBox_Drop"/>
            <TextBlock Name="status"
                       Text="No dragging"/>
        </StackPanel>
    </Window>
    

    Code:

    public partial class DnDTestBasic : Window
    {
        public DnDTestBasic()
        {
            InitializeComponent();
        }
    
        private Point dragStartPoint;
    
        private void button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            dragStartPoint = e.GetPosition(null);
            status.Text = "New drag start position";
        }
    
        private static bool IsDragging(Point dragStartPoint, MouseEventArgs e)
        {
            var diff = e.GetPosition(null) - dragStartPoint;
            return
                e.LeftButton == MouseButtonState.Pressed &&
                (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
                 Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance);
        }
    
        private void button_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (IsDragging(dragStartPoint, e))
            {
                status.Text = "Starting drag...";
                DragDrop.DoDragDrop(button, new DataObject("Button", button), DragDropEffects.Copy);
                status.Text = "Dragging done.";
                e.Handled = true;
            }
        }
    
        private void textBox_Dragging(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent("Button"))
                e.Effects = DragDropEffects.Copy;
            else
                e.Effects = DragDropEffects.None;
            e.Handled = true;
        }
    
        private void textBox_Drop(object sender, DragEventArgs e)
        {
            var button = (Button)e.Data.GetData("Button");
            textBox.Text = string.Format("[{0}]", button.Content.ToString());
            e.Handled = true;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple question. I want to test whether a particular port
I have an extremely simple unit test. I'm not sure why it fails when
Based on a simple test I ran, I don't think it's possible to put
I'm trying to do a simple test php script for sessions. Basically it increments
I have written the following simple test in trying to learn Castle Windsor's Fluent
I have recorded a very simple test case Using the Selenium IDE integrated with
I'm evaluating subversion's branch/merge capabilities, and I decided to do a simple test -
I have a simple little test app written in Flex 3 (MXML and some
Are there conventions for function names when using the Perl Test::More or Test::Simple modules?
What is a good (and preferably simple) way to test the rendering performance of

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.