I want to get the mouse position when the mouse moves while dragging.
Here is my minimal not working example:
MainWindow.xaml:
<Window x:Class="WpfApplication8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Canvas x:Name="_canvas" Background="Green">
<TextBlock x:Name="_positionTextBlock"/>
<Rectangle x:Name="_dragSource"
Width="20"
Height="50"
Fill="Blue"
Canvas.Top="150"
Canvas.Left="20"
PreviewMouseDown="DoDrag"/>
<Rectangle x:Name="_dragTarget"
Width="60"
Height="70"
Fill="Red"
Canvas.Top="110"
Canvas.Left="300"
AllowDrop="True"/>
</Canvas>
</Window>
MainWindow.xaml.cs:
using System.Windows;
using System.Windows.Input;
namespace WpfApplication8
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void DoDrag(object sender, MouseButtonEventArgs e)
{
DragDrop.DoDragDrop(_dragSource, "abc", DragDropEffects.Move);
}
}
}
As you can see: I have a TextBlock
called _positionTextBlock
which should display the current mouse position while dragging.
Edit (long story): I have a green rectanlge that can be dragged and droped on the red rectangle. My requirement is, to display the mouse position while dragging the green rectangle.
How do I do that?
What I have tried:
changing the constructor of MainWindow
to:
public MainWindow()
{
InitializeComponent();
DragOver += (s, e) => _positionTextBlock.Text = e.GetPosition(_canvas).ToString();
}
But this only shows the position when the mouse is over the red rectangle.
I also tried:
public MainWindow()
{
InitializeComponent();
MouseMove += (s, e) => _positionTextBlock.Text = e.GetPosition(_canvas).ToString();
}
but the MouseMove
-Event doesn't fire while dragging
I also tried:
private void DoDrag(object sender, MouseButtonEventArgs ev)
{
_dragSource.GiveFeedback +=
(s, e) => _positionTextBlock.Text = Mouse.PrimaryDevice.GetPosition(_canvas).ToString();
DragDrop.DoDragDrop(_dragSource, "abc", DragDropEffects.Move);
}
but this seems to get the window-position in screen coordinates.
And this is where I got stuck. Does anyone have any idea?
GiveFeedback
event, noGetPosition()
– Rico-E