简体   繁体   English

在两个WPF控件之间拖放-我需要根据第二个控件内的位置更改鼠标图标

[英]Dragging and dropping between two wpf controls - I need the mouse icon to change depending on location inside 2nd control

I have two WPF controls. 我有两个WPF控件。 One is a TreeView and the other is a graph control. 一个是TreeView,另一个是图形控件。 I have dragging and dropping working between them. 我在他们之间拖放工作。 When I drag from the TreeView control to the graph control and drop something it works as I want it to. 当我从TreeView控件拖动到图形控件并放下某些东西时,它可以按我的意愿工作。 The mouse cursor has the dragging drop look to it during this. 在此期间,鼠标光标具有拖放外观。 However I want to change the mouse cursor (to something that points up) if the user points the mouse to the top half of the graph control. 但是,如果用户将鼠标指向图形控件的上半部分,则想更改鼠标光标(指向上方的内容)。 If the user goes to the bottom of the graph control then I want the cursor to go back to the original dragging drop look. 如果用户转到图形控件的底部,那么我希望光标返回到原始的拖放外观。

I thought I could use the GiveFeedback event with the 1st control but that doesn't return the graph object to me. 我以为可以在第一个控件中使用GiveFeedback事件,但这不会将图形对象返回给我。

I can provide code if needed but I don't think it would be helpful. 我可以根据需要提供代码,但我认为这不会有所帮助。 I do have a method called MouseNearTop(Graph g, DragEventArgs e) that returns a bool true if the mouse is in the top half of the grid ad false if on the bottom half. 我确实有一个称为MouseNearTop(Graph g,DragEventArgs e)的方法,如果鼠标位于网格的上半部分,则返回布尔值true;如果位于下半部分,则返回false。

UPDATE: I tried using the Mouse.OverrideCursor property but that seems to change the mouse after you release the button. 更新:我尝试使用Mouse.OverrideCursor属性,但是在您释放按钮后似乎改变了鼠标。 I tried again using the static class DragDrop but that throws exceptions and still doesn't work. 我再次尝试使用静态类DragDrop,但是会引发异常,但仍然无法正常工作。

This is for the code for my second attempt: 这是我第二次尝试的代码:

namespace WpfApplication11
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private bool pointingUp = true;

    private void Rectangle_DragOver(object sender, DragEventArgs e)
    {
        currentPoint = e.GetPosition(MyRectangle);

        if ((currentPoint.X > 0) && (currentPoint.X < MyRectangle.ActualWidth) && (currentPoint.Y > 0) && (currentPoint.Y < (MyRectangle.ActualHeight / 2)))
        {
            if (!pointingUp)
            {
                //Mouse.OverrideCursor = Cursors.UpArrow;'
                try
                {
                    DragDrop.DoDragDrop(MyRectangle1, MyRectangle1, DragDropEffects.Copy);
                }
                catch
                {

                }
                pointingUp = true;
            }
        }
        else
        {
            if (pointingUp)
            {
                //Mouse.OverrideCursor = null;
                try
                {
                    DragDrop.DoDragDrop(MyRectangle1, MyRectangle1, DragDropEffects.Move);
                }
                catch
                {

                }
                pointingUp = false;
            }
        }
    }

    Point currentPoint = new Point();

    private void MyRectangle1_MouseMove(object sender, MouseEventArgs e)
    {
        System.Media.SystemSounds.Beep.Play();
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            //if (FileTree.SelectedItem == null)
            //{
            //  return;
            //}

            //var node = FileTree.SelectedItem as TreeViewNode;
            // && (node.TableName.Equals("Ttmp", StringComparison.InvariantCultureIgnoreCase))
            //if ((node.Items.Count == 0) && !(node.TableName == "Temp" || node.NodeDisplayName.EqualsAtLeastOne(StringComparison.InvariantCultureIgnoreCase, "DiFR", "DSFR")))
            //{
                var mousePos = e.GetPosition(null);
                var diff = _startPoint - mousePos;

                if (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance
                    || Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
                {
                    DragDrop.DoDragDrop(MyRectangle1, MyRectangle1, DragDropEffects.Move);
                }
            //}
        }
    }

    private Point _startPoint;

    private void MyRectangle1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        _startPoint = e.GetPosition(null);
    }

    private void MyRectangle_GiveFeedback(object sender, GiveFeedbackEventArgs e)
    {
        //Point p = e.GetPosition(MyRectangle);



    }

    private void MyRectangle_Drop(object sender, DragEventArgs e)
    {
        e.Effects = DragDropEffects.None;
    }


}
}

I've used a Rectangle for demo purposes, but I think this should work OK with whatever graph you're using. 我已经将Rectangle用于演示目的,但是我认为这对您使用的任何图形都可以正常工作。

private void MyRectangle_DragOver(object sender, DragEventArgs e)
{
    Point p = e.GetPosition(MyRectangle);
    if ((p.X > 0) && (p.X < MyRectangle.ActualWidth) && (p.Y > 0) && (p.Y < (MyRectangle.ActualHeight / 2)))
    {
        Mouse.OverrideCursor = Cursors.UpArrow;
    }
    else
    {
        Mouse.OverrideCursor = null;
    }
}

I figured a solution. 我想出了一个解决方案。

In the object I am dragging the data into I have this method: 在对象中,我将数据拖到具有此方法的地方:

        private void ObjectDraggingInto_DragOver(object sender, DragEventArgs e)
    {
        if (ObjectDraggingFrom.DragDroppingOn)
        {
            ObjectDraggingFrom.MoveUpCursor = MouseNearTop(sender, e)
                ? true
                : false;
        }
    }

Then here is the code from the object that I started dragging from: 这是我开始从中拖动对象的代码:

    public static bool MoveUpCursor = false;
    public static bool DragDroppingOn = false;

    private void ObjectDraggingFrom_GiveFeedback(object sender, GiveFeedbackEventArgs e)
    {
        if (MoveUpCursor)
        {
            e.UseDefaultCursors = false;
            Mouse.SetCursor(Cursors.UpArrow);
        }
        else
        {
            e.UseDefaultCursors = true;
        }

        e.Handled = true;
    }


        private void ObjectDragging_Drop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            ((DataExploreViewModel)DataContext).ImportDraggedAndDroppedFiles((string[])e.Data.GetData(DataFormats.FileDrop));
        }

        DragDroppingOn = false;
    }



        private void ObjectDraggingFrom_MouseMove(object sender, MouseEventArgs e)
    {

        if (e.LeftButton == MouseButtonState.Pressed)
        {
            if (myData.SelectedItem == null)
            {
                return;
            }


                var mousePos = e.GetPosition(null);
                var diff = _startPoint - mousePos;

                if (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance
                    || Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
                {
                    DragDroppingOn = true;
                    DragDrop.DoDragDrop(FileTree, data, DragDropEffects.Move | DragDropEffects.Copy);
                }
            }
    }

So this will change the mouse drag cursor to a up arrow when you are in the upper half of the object you are dragging to. 因此,当您在拖动对象的上半部分时,这会将鼠标拖动光标更改为向上箭头。 Otherwise the cursor will look like the normal drag cursor. 否则,光标将看起来像普通的拖动光标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM