简体   繁体   English

拖放时如何获取MousePosition? (WPF-MVVM)

[英]How to get the MousePosition while Drag&Drop? (WPF-MVVM)

So basically I followed this example to create my own Drag&Drop. 因此,基本上,我按照示例创建了自己的拖放。 I want to be able to drag an Object from one View to another (UserControls inside MainWindow) and place it at the current mouse location relative to the view in which I dropped my Element. 我希望能够将一个对象从一个视图拖到另一个视图(MainWindow内的UserControls),并将其放置在相对于放置我的Element的视图的当前鼠标位置。

I tried using this code in my FrameworkElementBehavior: 我尝试在FrameworkElementBehavior中使用以下代码:

private void AssociatedObject_Drop(object sender, DragEventArgs e)
    {
        CanItDropHere(e,
            canDrop: () =>
            {
                var type = _allowedDropTypes.First(t => e.Data.GetDataPresent(t));
                var draggedItem = e.Data.GetData(type) as IDragable;
                var position = e.GetPosition(App.Current.MainWindow);

                var droppedOn = AssociatedObject.DataContext as IDropableWithMousePos;
                droppedOn?.Drop(draggedItem,position);
            });


        e.Handled = true;
    }

However here I get the Mouse Position relative to my MainWindow. 但是在这里,我得到了相对于MainWindow的鼠标位置。 This leads to the problem that the object gets dopped with an offset which is obvious. 这就导致了这样一个问题,即对象被掺杂了明显的偏移量。 However I have no clue how to get the Position realtive to the view in which I drop. 但是,我不知道如何将位置实现为所放置的视图。

工作解决方案:

var position = e.GetPosition(sender as IInputElement);

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

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