WPF: Binding ContextMenu to Visual Parent

I know that ContextMenu are not part of the visual tree, but I tried to bind the Visibility property of the ContextMenu property to the property in its parent UserControl . So far, I have tried to bind ancestors and experimented with the transformed, but the only way that seems remote at any time is to use the MultiValueConverter and reflection to pass type names and property names to UserControl . Even then, I'm not sure if this will work.

Am I going about it wrong?

+1
source share
2 answers

the context menu is displayed only when you right-click, so you do not need to set its visibility. perhaps you should use a popup.

however, if you want to make some funky bindings, you can use the spy element from the excellent josh smith to use element name bindings to snap between things that are usually possible.

+3
source

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

 <ContextMenu Visibility="{Binding PlacementTarget.Visibility, RelativeSource={RelativeSource Self}}" /> 
0
source

Source: https://habr.com/ru/post/1309930/


All Articles