Search code examples
wpfuser-controlscontextmenu

WPF UserControl Context Menu Visibility Binding


My user control context menu visibility can't bind a dependency property. Any idea?

Here is my WPF code

<UserControl.ContextMenu>
    <ContextMenu Visibility="{Binding ElementName=wellControl, Path=IsInCompactMode, Converter={StaticResource BooleanToVisibilityConverter}}">
    <MenuItem Command="local:GCommands.Edit" />
    <MenuItem Command="local:GCommands.Delete" />
    <MenuItem Command="local:GCommands.ExportFcsFiles" />
    <MenuItem Command="local:GCommands.BatchExportStatistics" />
    <Separator/>
    <MenuItem Command="local:GCommands.SaveAs" Header="Save As..." />
    </ContextMenu>
</UserControl.ContextMenu>

If I set Visibility="Hidden" it will work for me. like this:

<ContextMenu Visibility="Hidden"/>

If use this it won't work

<ContextMenu Visibility="{Binding ElementName=wellControl, Path=IsInCompactMode, Converter={StaticResource BooleanToVisibilityConverter}}">

I'm pretty sure Visibility="{Binding ElementName=wellControl, Path=IsInCompactMode, Converter={StaticResource BooleanToVisibilityConverter}}" has not problem, cuz it works for others.

here is my dependency property

public bool IsInCompactMode
        {
            get {return (bool)GetValue(IsInCompactModeProperty); }
            set {SetValue(IsInCompactModeProperty, value); }
        }
        public static readonly DependencyProperty IsInCompactModeProperty =
            DependencyProperty.Register("IsInCompactMode", typeof(bool), typeof(WellControl), new PropertyMetadata(false));

I tried this way, it seems it still doesn't work, this is really weird!!!

<ContextMenu x:Name="menu" IsOpen="{Binding ElementName=wellControl, Path=IsInCompactMode}"> 

I'm really confuse about it, what's wrong? "Binding ElementName=wellControl, Path=IsInCompactMode" works for other part of the user control, just doesn't work for the weird context menu? it doesn't make sence


Solution

  • 1> ContextMenu, Popups, DataGridColumns are not part of visual tree. So the binding using ElementName or RelativeSource wont work just like that.

    2> When you want the context menu to not display under particular situation use the triggers to unset the context menu from the visual itself.

          <TextBlock Text="ContextMenu is not shown when DataContext.IsShow is false"}">
                <TextBlock.Style>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="ContextMenu"
                                Value="{StaticResource TextBlockContextMenu}" />
                       <Style.Triggers>
                           <DataTrigger Binding="{Binding IsShow}"
                                        Value="False">
                                <Setter Property="ContextMenu"
                                        Value="{x:Null}" />
                           </DataTrigger>   
                       </Style.Triggers>
                    </Style>
                </TextBlock.Style>
          </TextBlock>
    

    3> To attach these items to the visual tree, so that binding works, we use the proxy element method...

    Bind datagrid column visibility MVVM

    I prefer second step.