将所选项目与鼠标一起拖动到WPF中 [英] Dragging selected Item along with the mouse in WPF

查看:115
本文介绍了将所选项目与鼠标一起拖动到WPF中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码来执行Drag&放在我的StackPanel Childran中,

I am using the following code for performing Drag & Drop in my StackPanel Childran,

XAML

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="601" Width="637">

<StackPanel Name="sp" AllowDrop="True" Background="SkyBlue" PreviewMouseLeftButtonDown="sp_PreviewMouseLeftButtonDown" PreviewMouseLeftButtonUp="sp_PreviewMouseLeftButtonUp" PreviewMouseMove="sp_PreviewMouseMove"
            DragEnter="sp_DragEnter" Drop="sp_Drop">
    <Image Source="/Assets/Image1.jpg" Height="100" Width ="100"/>
    <Image Source="/Assets/Image2.jpg" Height="100" Width ="100"/>
    <Image Source="/Assets/Image3.jpg" Height="100" Width ="100"/>
    <Image Source="/Assets/Image4.jpg" Height="100" Width ="100"/>
    <Image Source="/Assets/Image5.jpg" Height="100" Width ="100"/>
</StackPanel>



Code Behind


public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }
    private bool _isDown;
    private bool _isDragging;
    private Point _startPoint;
    private UIElement _realDragSource;
    private UIElement _dummyDragSource = new UIElement();




    private void sp_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if (e.Source == this.sp)
        {
        }
        else
        {              
            _isDown = true;                             
            _startPoint = e.GetPosition(this.sp);                       
        }
    }

    private void sp_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        _isDown = false;
        _isDragging = false;
        _realDragSource.ReleaseMouseCapture();
    }

    private void sp_PreviewMouseMove(object sender, MouseEventArgs e)
    {          
        if (_isDown)
        {
            if ((_isDragging == false) && ((Math.Abs(e.GetPosition(this.sp).X - _startPoint.X) > SystemParameters.MinimumHorizontalDragDistance) ||
                (Math.Abs(e.GetPosition(this.sp).Y - _startPoint.Y) > SystemParameters.MinimumVerticalDragDistance)))
            {
                _isDragging = true;
                _realDragSource = e.Source as UIElement;
                _realDragSource.CaptureMouse();
               DragDrop.DoDragDrop(_dummyDragSource, new DataObject("UIElement", e.Source, true), DragDropEffects.Move);
            }              
        }
    }

    private void sp_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent("UIElement"))
        {
            e.Effects = DragDropEffects.Move;               
        }
    }

    private void sp_Drop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent("UIElement"))
        {              
            UIElement droptarget = e.Source as UIElement;              
            int droptargetIndex=-1, i =0;
            foreach (UIElement element in this.sp.Children)
            {
                if (element.Equals(droptarget))
                {
                    droptargetIndex = i;
                    break;
                }
                i++;
            }
            if (droptargetIndex != -1)
            {
                this.sp.Children.Remove(_realDragSource);
                this.sp.Children.Insert(droptargetIndex, _realDragSource);
            }

            _isDown = false;
            _isDragging = false;
            _realDragSource.ReleaseMouseCapture();
        }
    }  
}

我正在努力实现是,我需要拖动单击并拖动点击的项目。在这个实现中,当拖放一个小的矩形点缀像选择出现,然后它下降到鼠标指针离开的地方。如何保持该图像与我的选择(拖放)

What i am trying to implement is, I need to drag the clicked item along with my click and drag. In this implementation, While drag and drop a small rectangle dotted like selection appears and then it drops to the place where mouse pointer leaves. How can i hold that image along with my selection (drag & drop)

感谢提前,

StebPet。

推荐答案

如果我正确理解您的意图,并希望对拖动的对象进行一些视觉反馈并放弃操作,那么您需要使用 Adorner Layer 。链接页面:

If I understand you correctly and you want to have some visual feedback of the object that is being dragged in a drag and drop operation, then you'll need to use the Adorner Layer. From the linked page:


Adorners是一种特殊类型的FrameworkElement,用于为用户提供视觉提示... [和]是在AdornerLayer中呈现,该渲染表面始终位于装饰元素的顶部

Adorners are a special type of FrameworkElement, used to provide visual cues to a user... [and] are rendered in an AdornerLayer, which is a rendering surface that is always on top of the adorned element

您可以找到一篇很好的文章,说明如何使用 WPF:拖放Adorner中的代码示例来执行此操作发布在Code Blitz上。

You can find a good article explaining how to do this with a code example in the WPF: Drag Drop Adorner post on Code Blitz.

这篇关于将所选项目与鼠标一起拖动到WPF中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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