2011-10-14 6 views
6

私は、メインウィンドウの次のXAMLがありますなぜWPF 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" /> 
    </Grid> 
</Window> 

をしかし、私は、ウィンドウにファイルをドラッグしようとすると、ドロップは許可されていません。 CanvasがListBoxに変更されると、すべてが完全に機能します。

キャンバスにドロップできるようにコードを変更するにはどうすればよいですか?デフォルトでは

答えて

22

Canvasはそれほどヒットテストは、カーソルがCanvas要素の上にあることを拾っていないバックグラウンドを持っていないが、代わりにドロップすることはできませんGridまたはWindowまでバブリングされます。次のようにTransparentに背景を設定し、それが動作するはずです:

<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> 
0

これは魔法のように動作します!コードでは、次のようなことをしたいと考えています:

Canvas myCanvas = new Canvas(); 

myCanvas.AllowDrop = true; 
myCanvas.Background = System.Windows.Media.Brushes.Transparent; 
関連する問題