为什么 WPF Canvas 不允许掉落?
我的主窗口有以下 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 时,一切正常。
如何更改代码以允许拖放到画布上?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,
Canvas
没有背景,因此命中测试不会检测到光标位于Canvas
元素上,而是冒泡到Grid< /code> 或
Window
不允许放置。将背景设置为透明
,如下所示,它应该可以工作:By default,
Canvas
has no background so hit-testing is not picking up that the cursor is over theCanvas
element, but is instead bubbling up to theGrid
orWindow
which don't allow drop. Set the background toTransparent
as follows and it should work:这就像一个魅力!在代码中,您可能想要执行以下操作:
This works like a charm! In code you would want to do something such as: