Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this WPF code generate a context menu?

Tags:

c#

.net

wpf

brushes

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>
like image 389
corey broderick Avatar asked May 16 '09 03:05

corey broderick


1 Answers

It's because the Transparent brush allows an area to be hittable and thus receive and respond to mouse clicks, whereas the default null 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.

like image 94
John Feminella Avatar answered Oct 21 '22 01:10

John Feminella