为什么 WPF Canvas 不掉线? [英] Why doesn't WPF Canvas alow drop?

查看:7
本文介绍了为什么 WPF Canvas 不掉线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主窗口有以下 XAML:

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>

但是当我尝试将文件拖到窗口时,不允许放置.将 Canvas 改为 ListBox 后,一切正常.

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?

推荐答案

默认情况下,Canvas 没有背景,所以当光标在 Canvas 元素,而是冒泡到不允许放置的 GridWindow .将背景设置为 Transparent 如下,它应该可以工作:

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>

这篇关于为什么 WPF Canvas 不掉线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆