为什么此 WPF 代码不生成上下文菜单?
也许我正在经历一个后鲍尔默巅峰时刻。 我希望有人能帮助我指出显而易见的事情。
为什么此代码在右键单击时生成上下文菜单:
<Canvas Background="Transparent">
<Canvas.ContextMenu>
<ContextMenu>
<TextBlock>WTF?</TextBlock>
</ContextMenu>
</Canvas.ContextMenu>
</Canvas>
并且此代码不生成上下文菜单:
<Canvas>
<Canvas.ContextMenu>
<ContextMenu>
<TextBlock>WTF?</TextBlock>
</ContextMenu>
</Canvas.ContextMenu>
</Canvas>
Perhaps I'm having a Post-Ballmer-Peak Moment.
I'm hoping that someone can help point out the obvious to me.
Why does this code generate a context menu on right click:
<Canvas Background="Transparent">
<Canvas.ContextMenu>
<ContextMenu>
<TextBlock>WTF?</TextBlock>
</ContextMenu>
</Canvas.ContextMenu>
</Canvas>
And this code doesn't generate a context menu:
<Canvas>
<Canvas.ContextMenu>
<ContextMenu>
<TextBlock>WTF?</TextBlock>
</ContextMenu>
</Canvas.ContextMenu>
</Canvas>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为
Transparent
画笔允许某个区域可点击,从而接收并响应鼠标点击,而默认的null
画笔则不允许。 换句话说,如果没有定义任何画笔,该区域会变成“空心”并且点击会通过; 如果定义了画笔(即使是透明的),它们是“实心”的并且可以接收点击声。请参阅有关 WPF 画笔的有用文章更多信息。
It's because the
Transparent
brush allows an area to be hittable and thus receive and respond to mouse clicks, whereas the defaultnull
brush doesn't. In other words, without any brush defined, the region becomes "hollow" and clicks pass through; with a brush defined (even a transparent one), they are "solid" and clicks can be received.See this helpful article on WPF brushes for more info.