Thursday, 4 October 2007

xaml context menu not showing


Context menus in xaml are great, because it's to easy to customise their look and feel, however I've just spent a couple of hours trying to debug a silly problem.

I'd set up a Canvas holder with some nested Canvas items, and I was trying to add context menus to each of the nested Canvas items. The menu appeared for the holder but not for the nested items, and so I assumed there was a problem with the ordering of the controls - perhaps somehow the z-order of the holder was higher than it's nested items?

After a lot of googling and frustration I found, by chance, that a control needs to have a Background Brush set before the context menu will show for the item... so you can have a correctly setup context menu wired up, but if the control doesn't have a brush, it ain't gonna show.

I'm not sure why this is - I'm sure there must be a logical design reason for it (perhaps someone thought people would need to see the control to click on it) but I found it a bit limiting!

I hope that saves someone, somewhere and hour or two.