corvus
corvus

Reputation: 2406

Why doesn't WPF Canvas alow drop?

I have the following XAML for the main window:

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

But when I try to drag a file to the window, drop is not allowed. When Canvas is changed to ListBox, everything works perfectly.

How can the code be changed to allow drop to canvas?

Upvotes: 8

Views: 3118

Answers (2)

Dan Milligan
Dan Milligan

Reputation: 121

This works like a charm! In code you would want to do something such as:

Canvas myCanvas = new Canvas();

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

Upvotes: 0

jeffora
jeffora

Reputation: 4159

By default, Canvas has no background so hit-testing is not picking up that the cursor is over the Canvas element, but is instead bubbling up to the Grid or Window which don't allow drop. Set the background to Transparent as follows and it should work:

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

Upvotes: 27

Related Questions