gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 19:48:10 28 4
gpt4 key购买 nike

我正在使用以下代码在我的 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>

背后的代码

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

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

提前致谢,

斯泰兹宠物。

最佳答案

如果我对您的理解正确,并且您想对在拖放操作中被拖动的对象有一些视觉反馈,那么您需要使用 Adorner Layer .从链接页面:

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: Drag Drop Adorner 中找到解释如何使用代码示例执行此操作的好文章。在 Code Blitz 上发帖。

关于wpf - 在WPF中用鼠标拖动选定的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22641809/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com