在两个WPF控件之间拖动和掉落 - 我需要鼠标图标可以根据第二个控件内的位置进行更改



我有两个WPF控件。一个是树景,另一个是图形控件。我之间有拖动和掉落。当我从TreeView控件拖动到图形控件并删除它所需的功能。在此期间,鼠标光标具有拖动滴的外观。但是,如果用户将鼠标指向图形控件的上半部分,我想更改鼠标光标(指向指向的内容)。如果用户转到图形控件的底部,那么我希望光标返回原始拖放掉落外观。

我以为我可以将givefeedback事件与第一个控件一起使用,但这不会将图对象返回给我。

我可以提供代码,但我认为这不会有帮助。我确实有一种称为MousenEartop(图G,DrageVentargs e)的方法,如果鼠标在网格广告的上半部分,则返回bool true,如果在下半部分。

更新:我尝试使用Mouse.overridecursor属性,但释放按钮后似乎会更改鼠标。我再次尝试使用静态类Dragdrop,但会引发异常,但仍然不起作用。

这是我第二次尝试的代码:

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 think 这应该可以与您使用的任何图表一起工作。

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

我发现了一个解决方案。

在对象中,我将数据拖动到我有此方法中:

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

那么,这是我开始从:

开始拖动的对象的代码
    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);
                }
            }
    }

因此,当您在要拖动的对象的上半部分时,这将将鼠标拖放光标变为向上箭头。否则,光标看起来像普通的拖放光标。

最新更新