0

编辑:我简化了问题,因为即使是最基本的应用程序也在做同样的事情

<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 或 Drop 事件都不会触发

编辑: 我添加了一个 Window_Drop 事件,这就是接受 Drop 事件的原因,Anyidea 为什么会这样,以及如何让 Canvas 接受它。我什至应该关心,我认为我可以使用它。

4

2 回答 2

1

Background如果您设置画笔,事件将触发。

<Canvas Name="C1" AllowDrop="True"
        DragEnter="Grid_DragEnter" Drop="Grid_Drop"
        Background="Transparent">
于 2012-05-18T02:06:41.937 回答
0

我并不完全清楚您想要做什么,但是如果您在图像上单击并拖动,将图像上的 AllowDrop 设置为 true 应该会导致您的 Grid_DragEnter 事件触发。

于 2012-05-17T23:58:51.583 回答