WPF UserControl Context Menu Visibility Binding

白昼怎懂夜的黑 提交于 2019-12-30 09:55:08

问题


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


回答1:


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.




回答2:


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




回答3:


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>


来源:https://stackoverflow.com/questions/7781932/wpf-usercontrol-context-menu-visibility-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!