WPF中的DragDrop问题 [英] Problem with DragDrop in wpf

查看:328
本文介绍了WPF中的DragDrop问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Wpf的新手,我想在多个wpf文本框之间创建拖放操作.我找到了很多解决方案,但它们不能满足我的要求...

我已经完成了类似的代码.

C#代码

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

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);
        }
    }
}





<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

推荐答案

此处 [ ^ ].首先,您没有处理正确的事件.
Here[^]. You''re not handling the right events, for a start.


这篇关于WPF中的DragDrop问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆