views:

420

answers:

2

I know ContextMenus aren't part of the visual tree, but I've been trying to bind the Visibility property of a contextmenu to a property on it's parent UserControl. So far I've tried ancestor binding and experimented with a converted, but the only way that seems remotely feasible at the minute is to use a MultiValueConverter and reflection to pass the type names and property names on the UserControl. Even then I'm not sure it would work.

Am I going about this the wrong way?

+3  A: 

a context menu is only visible when you right click, so you should not need to set its visibility. maybe you should be using a popup.

however if you want to do some funky bindings you can use element spy from the excellent josh smith to use element name bindings to bind between things that arent usually possible.

Aran Mulholland
That's a really great helper class. Thanks.
Echilon
@Aran Thankyou, Thankyou, Thankyou, Thankyou, Thankyou, Thankyou. I found this question/answer while looking for a solution to something else. `ElementSpy` is EXACTLY what I needed. +1 for sure.
Aren
A: 

I'm not sure why you would need to bind the visibility of a ContextMenu... but anyway, here's a solution that should work :

<ContextMenu Visibility="{Binding PlacementTarget.Visibility, RelativeSource={RelativeSource Self}}" />
Thomas Levesque