拖放-在WPF中用鼠标拖动选定的项目



我使用以下代码执行拖动&拉入我的StackPanel child,

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>

背后的代码
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();
        }
    }  
}

我要实现的是,我需要拖动点击项目随着我的点击和拖动。在这个实现中,当拖放时,会出现一个小矩形点状的选择,然后它会下降到鼠标指针离开的地方。如何将图像与选区保持在一起(拖动&下降)

Thanks in Advance,

StezPet .

如果我理解正确,并且您希望在拖放操作中对被拖放的对象进行一些视觉反馈,那么您需要使用装饰层。从链接页面:

装饰器是一种特殊类型的FrameworkElement,用于向用户提供视觉提示…[和]在装饰层中渲染,这是一个渲染表面,总是在装饰元素的顶部

您可以在code Blitz上的WPF: Drag Drop Adorner帖子中找到一篇很好的文章,用代码示例解释如何做到这一点。

您需要创建一个Adorner来显示您的项目。

Adorner加入DragDrop.DoDragDrop之前的AdornerLayer中。

重写PreviewDragOver以便在拖动过程中更新Adorner的位置。

DragDrop.DoDragDrop之后的AdornerLayer中删除Adorner

相关内容

  • 没有找到相关文章

最新更新