Why doesn't WPF Canvas crash?

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 the file into the window, the drop is not allowed. When Canvas is changed to ListBox, everything works fine.

How can I change the code so that I can leave it on the canvas?

+6
source share
2 answers

By default, Canvas does not have a background, so a hit check does not raise the cursor over the Canvas element, but instead bubbles up to the Grid or Window , which does not allow it to go down. Set the Transparent background 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> 
+22
source

It works like a charm! In the code, you would like to do something like:

 Canvas myCanvas = new Canvas(); myCanvas.AllowDrop = true; myCanvas.Background = System.Windows.Media.Brushes.Transparent; 
0
source

Source: https://habr.com/ru/post/899270/


All Articles