为什么';WPF帆布低落差?

为什么';WPF帆布低落差?,wpf,visual-studio-2008,drag-and-drop,Wpf,Visual Studio 2008,Drag And Drop,我的主窗口有以下XAML: <Window x:Class="ImageViewer.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="398" Width="434"> <Grid>

我的主窗口有以下XAML:

<Window x:Class="ImageViewer.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   Title="Window1" Height="398" Width="434">
   <Grid>
      <Canvas AllowDrop="True" />
   </Grid>
</Window>

但当我尝试将文件拖到窗口时,不允许拖放。当Canvas更改为ListBox时,一切都可以完美地工作


如何更改代码以允许拖放到画布?

默认情况下,
canvas
没有背景,因此命中测试不会检测到光标位于
canvas
元素上,而是冒泡到不允许拖放的
网格或
窗口。将背景设置为透明,如下所示,它应该可以工作:

<Window x:Class="ImageViewer.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   Title="Window1" Height="398" Width="434">
   <Grid>
      <Canvas AllowDrop="True" Background="Transparent" />
   </Grid>
</Window>

这就像一个符咒!在代码中,您可能希望执行以下操作:

Canvas myCanvas = new Canvas();

myCanvas.AllowDrop = true;
myCanvas.Background = System.Windows.Media.Brushes.Transparent;