tags:

views:

461

answers:

1

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>
+7  A: 

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
I retract my previous WTF statement. That makes perfect sense. +1
daub815
That's really good to know, actually. And it actually makes sense why it does that.Thanks
Joel
No problem! Glad to be of help.
John Feminella