7

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>
John Feminella
  • 303,634
  • 46
  • 339
  • 357
corey broderick
  • 185
  • 2
  • 7
  • WTF, indeed. I thought you were crazy at first, but I got the same result. WPF baffles me sometimes also. Hopefully, there is some sane reason for this. – kevindaub May 16 '09 at 03:33

1 Answers1

17

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.

John Feminella
  • 303,634
  • 46
  • 339
  • 357