Click here to Skip to main content
16,023,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I m new with Wpf I want to create a drag drop operations between multiple wpf textbox. i have found many solution but they are not fulfill my requirement...

I have done code like.

C# code

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace DragDrop
{
    public partial class Window1 : Window
    {
        TextBox dragSource = null;
        public Window1()
        {
            InitializeComponent();
        }
        private void txt1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            TextBox parent = (TextBox)sender;
            dragSource = parent;
            DataObject dragData = new DataObject(typeof(string), sender as TextBox);
            if (dragData != null)
            {
                System.Windows.DragDrop.DoDragDrop(parent, dragData, DragDropEffects.Move);
            }
        }

        private void txt1_PreviewDrop(object sender, DragEventArgs e)
        {
            var dropedData = e.Data.GetData(typeof(string));
            MessageBox.Show(dropedData.ToString());
            var sourceElement = (FrameworkElement)sender;
            var target = (sourceElement.InputHitTest(e.GetPosition(sourceElement)) as FrameworkElement);
        }
    }
}


HTML
<Window x:Class="DragDrop.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="400" Width="525">
    <Grid>
        <Canvas x:Name="canV" >
            <Label Canvas.Left="100" Canvas.Top="80" Content="Column A"></Label>
            <Label Canvas.Left="300" Canvas.Top="80" Content="Column B"></Label>
            <TextBox x:Name="txt1" Canvas.Left="100" Canvas.Top="120" Width="150" PreviewMouseLeftButtonDown="txt1_PreviewMouseLeftButtonDown" PreviewDrop="txt1_PreviewDrop"></TextBox>
            <TextBox Canvas.Left="300" Canvas.Top="120" Width="150" PreviewMouseLeftButtonDown="txt1_PreviewMouseLeftButtonDown" PreviewDrop="txt1_PreviewDrop"></TextBox>
            <TextBox Canvas.Left="100" Canvas.Top="160" Width="150" PreviewMouseLeftButtonDown="txt1_PreviewMouseLeftButtonDown" PreviewDrop="txt1_PreviewDrop"></TextBox>
            <TextBox Canvas.Left="300" Canvas.Top="160" Width="150" PreviewMouseLeftButtonDown="txt1_PreviewMouseLeftButtonDown" PreviewDrop="txt1_PreviewDrop"></TextBox>
            <TextBox Canvas.Left="100" Canvas.Top="200" Width="150" PreviewMouseLeftButtonDown="txt1_PreviewMouseLeftButtonDown" PreviewDrop="txt1_PreviewDrop"></TextBox>
            <TextBox Canvas.Left="300" Canvas.Top="200" Width="150" PreviewMouseLeftButtonDown="txt1_PreviewMouseLeftButtonDown" PreviewDrop="txt1_PreviewDrop"></TextBox>
            <TextBox Canvas.Left="100" Canvas.Top="240" Width="150" PreviewMouseLeftButtonDown="txt1_PreviewMouseLeftButtonDown" PreviewDrop="txt1_PreviewDrop"></TextBox>
            <TextBox Canvas.Left="300" Canvas.Top="240" Width="150" PreviewMouseLeftButtonDown="txt1_PreviewMouseLeftButtonDown" PreviewDrop="txt1_PreviewDrop"></TextBox>
        </Canvas>
    </Grid>
</Window>


pls help .any help will sufficient for me....
Thanks
Posted
Updated 8-Feb-12 13:47pm
v5

1 solution

Here[^]. You're not handling the right events, for a start.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900