WPF拖放一个按钮 [英] WPF drag and drop on a button
问题描述
我正在写有按钮的网格WPF应用程序,我想允许用户拖放网格之间的按钮,可能是应用程序的不同实例之间。我试着通过添加一个处理程序按钮上的previewMouseMove事件,然后调用的DoDragDrop如果左边鼠标按键时这样做,但是当我拖放按钮,它总是结束调用的DoDragDrop两次,降事件处理程序的两倍。有谁知道为什么出现这种情况,如何prevent呢?
I'm writing a WPF application that has grids of buttons and I want to allow the user to drag and drop buttons between grids, possibly between different instances of the application. I've tried doing this by adding a handler to the PreviewMouseMove event on the button and then calling DoDragDrop if the left mouse button is down, but when I drag and drop the button it always ends up calling DoDragDrop twice and the drop event handler twice. Does anyone know why this happens and how to prevent it?
下面是这表明了问题的一些例子XAML:
Here's some example XAML which demonstrates the problem:
<Window x:Class="WpfTest.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">
<DockPanel>
<Button PreviewMouseMove="PreviewMouseMove" x:Name="m_button" Width="250">
Hello, world!
</Button>
<Label Drop="Drop" AllowDrop="True">
Drop here!
</Label>
</DockPanel>
</Window>
和相应的code:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void PreviewMouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
++m_dragIndex;
System.Console.WriteLine("Dragged: " + m_dragIndex.ToString());
DragDrop.DoDragDrop(m_button, m_dragIndex, DragDropEffects.All);
e.Handled = true;
}
}
private void Drop(object sender, DragEventArgs e)
{
System.Console.WriteLine("Dropped: " + e.Data.GetData(typeof(Int32)).ToString());
}
private int m_dragIndex;
}
对于单拖,这被写入到输出:
For a single drag, this gets written to the output:
Dragged: 1
Dragged: 2
Dropped: 2
Dropped: 1
更新:我已经改变了例如code以上,以显示其下降当按钮被丢弃到的东西的事件被调用
UPDATE: I've changed the example code above to show which drop events get called when the button is dropped onto something.
更新:更新的问题,包括容器和应用程序实例之间拖动,因为这是使用的DragDrop系统的激励因素。
UPDATE: Updated the question to include dragging between containers and application instances, since this is the motivating factor for using the DragDrop system.
推荐答案
我已经找到了解决方法这一点 - 因为第一次的DoDragDrop内部调用第二previewMouseMove,我可以跟踪我是否已经是$内p $ pviewMouseMove呼叫,忽略它,如果我。这似乎有点讨厌,虽然所以我希望有一个更好的解决方案。
I've found a workaround for this - since the first DoDragDrop calls the second PreviewMouseMove internally, I can track whether I'm already inside a PreviewMouseMove call and ignore it if I am. This seems a bit nasty though so I'm hoping there's a better solution.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void PreviewMouseMove(object sender, MouseEventArgs e)
{
if(!m_inMouseMove)
{
m_inMouseMove = true;
if (e.LeftButton == MouseButtonState.Pressed)
{
++m_dragIndex;
System.Console.WriteLine("Dragged: " + m_dragIndex.ToString());
DragDrop.DoDragDrop(m_button, m_dragIndex, DragDropEffects.All);
e.Handled = true;
}
m_inMouseMove = false;
}
}
private void Drop(object sender, DragEventArgs e)
{
System.Console.WriteLine("Dropped: " + e.Data.GetData(typeof(Int32)).ToString());
}
private int m_dragIndex;
bool m_inMouseMove;
}
这篇关于WPF拖放一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!