WPF UserControl Context Menu Visibility Binding
Asked Answered
D

3

6

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

Disconcerted answered 16/10, 2011 at 2:21 Comment(3)
Why not? What happens? What are you trying to do?Affra
With this amount of information, nobody is going to be able to help you...Cunha
Sorry guys, I posted this by my cellphone didn't able to add more information.Disconcerted
D
14

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.

Directional answered 17/10, 2011 at 7:39 Comment(0)
S
1

If you are trying to show/hide a context menu you should use the IsOpen property of the ContextMenu, rather than the Visibility property.

Stepaniestepbrother answered 16/10, 2011 at 9:10 Comment(2)
but this way still doesn't work <ContextMenu x:Name="menu" IsOpen="{Binding ElementName=wellControl, Path=IsInCompactMode}">Disconcerted
this does not work. I have tested.Apportion
A
-1

You should add the Update Trigger : "UpdateSourceTrigger=PropertyChanged"

<DataGrid.ContextMenu >
    <ContextMenu Visibility="{Binding IsSelectionEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,Converter={StaticResource BooleanToVisibilityConverter}}" >
        <ContextMenu.Items>
            <MenuItem Header="Create Layer" Command="{Binding DefineLayerName }" />
        </ContextMenu.Items>
    </ContextMenu>
</DataGrid.ContextMenu>
Ancalin answered 16/11, 2017 at 9:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.