4

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

私が実装しようとしているのは、クリックしてドラッグしたアイテムと一緒にクリックしたアイテムをドラッグする必要があるということです。この実装では、ドラッグ アンド ドロップすると、点のような小さな四角形が選択範囲として表示され、マウス ポインターが離れた場所にドロップされます。選択した画像と一緒にその画像を保持するにはどうすればよいですか (ドラッグ & ドロップ)

前もって感謝します、

ステズペット。

4

2 に答える 2

1

正しく理解し、ドラッグ アンド ドロップ操作でドラッグされているオブジェクトの視覚的なフィードバックが必要な場合は、Adorner Layerを使用する必要があります。リンクされたページから:

Adorners は特別なタイプの FrameworkElement であり、ユーザーに視覚的な合図を提供するために使用されます... [そして] AdornerLayer でレンダリングされます。これは、常に装飾された要素の上にあるレンダリング サーフェスです。

Code BlitzのWPF: Drag Drop Adornerの記事で、コード例を使用してこれを行う方法を説明している優れた記事を見つけることができます。

于 2014-03-25T23:02:35.920 に答える
0

アイテムを表示する を作成する必要がAdornerあります。

前にを追加しAdornerます。AdornerLayerDragDrop.DoDragDrop

ドラッグ中PreviewDragOverの位置を更新するためにオーバーライドします。Adorner

afterAdornerから を削除します。AdornerLayerDragDrop.DoDragDrop

于 2014-03-25T23:01:53.177 に答える