WPF: Binding ContextMenu to visual parent
Asked Answered
A

2

1

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 its 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?

Aeronautics answered 12/1, 2010 at 18:7 Comment(0)
M
3

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.

Milkfish answered 12/1, 2010 at 22:34 Comment(2)
That's a really great helper class. Thanks.Aeronautics
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.Numbersnumbfish
T
0

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}}" />
Tannenberg answered 12/1, 2010 at 23:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.