編輯: 我簡化了問題,因爲連最基本的應用程序,在做同樣的事情拖放沒有響應預期
<Window x:Class="WpfApplication1.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" AllowDrop="True" >
<Canvas DragEnter="Grid_DragEnter" Drop="Grid_Drop" AllowDrop="True" Name="C1">
<Image Height="42" HorizontalAlignment="Left" Margin="27,28,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="42" Source="/WpfApplication1;component/Images/BackgroundMan.PNG" MouseDown="image1_MouseDown" AllowDrop="False" Canvas.Left="-15" Canvas.Top="-16" />
</Canvas>
</Window>
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void image1_MouseDown(object sender, MouseButtonEventArgs e)
{
//added a breakpoint it does fire
//C1 is the name of the canvas
DragDrop.DoDragDrop(C1, sender, DragDropEffects.Move);
}
private void Grid_DragEnter(object sender, DragEventArgs e)
{
//added a breakPoint it never fires
e.Effects = DragDropEffects.Move;
}
private void Grid_Drop(object sender, DragEventArgs e)
{
//added a breakPoint it never fires
image1.Margin = new Thickness(e.GetPosition(this).X, e.GetPosition(this).Y, 0, 0);
}
}
}
既不爲dragenter或跌落事件觸發
編輯: 我添加了一個Window_Drop事件,這就是接受Drop事件的原因,Anyidea爲什麼會這樣,以及如何讓Canvas接受它。如果我更關心,我可以用我的想法來工作。
可能你應該將窗體的AllowDrop屬性設置爲false。網格上是否有PreviewDragEnter事件?不記得:)如果是的話,嘗試添加一個處理程序到它 –