Sheamus
Sheamus

Reputation: 223

binding error to hide menu item

I want to hide a menuItem regarding user's rights. The menu item is placed into a context menu (displayed with a right click) into a userControl. Rights are passed to the user control trough the main window. I have an error 40 : Binding error. VS can't find my property declared in the xaml document.

MainWindow.xaml

<MyUC:myUC
    ...
    MainOptionsVisibility="False" />

myUc.xaml

<GMap_NET_WindowsPresentation:GMapControl.ContextMenu>
                <ContextMenu Opened="ContextMenu_Opened">
                    <MenuItem
                        Header="{x:Static Internationalization:Resources.VIEWPORT_ADDOBJECT}"
                        Command="{x:Static local:Viewport.CreateGraphicObjectRequestCommand}"
                        CommandTarget="{Binding Path=PlacementTarget,RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                        Visibility="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled, Converter={StaticResource BooleanToVisibilityCollapsedConverter}}">
                        <MenuItem.Icon>
                            <Image Source="{DynamicResource EditIcon}" Width="32" Height="32" />
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="{x:Static Internationalization:Resources.OPTIONS}"
                             Visibility="{Binding Path=MainOptionsVisibility, RelativeSource={RelativeSource Self}, Converter={StaticResource BooleanToVisibilityCollapsedConverter}}" >
                             ...
                     />
            </GMap_NET_WindowsPresentation:GMapControl.ContextMenu>
        </GMap_NET_WindowsPresentation:GMapControl>

And MainOptionsVisibility is well declared as dependency property in code-behind. I have checked, it is well initialized. The visibility of the other item is OK (I didn't do it).

EDIT : new XAML code after Flo's answer :

<UserControl
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:local="clr-namespace:Main.Client.MyProject.Implementation.UIs.StandardViewports.Viewports"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:GMap_NET_WindowsPresentation="clr-namespace:GMap.NET.WindowsPresentation;assembly=GMap.NET.WindowsPresentation"
         xmlns:Internationalization="clr-namespace:Main.Client.MyProject.Library.Resources;assembly=MyProjectLibrary"
         xmlns:Main_Client_MyProject_Library_Converters="clr-namespace:Main.Client.MyProject.Library.Converters;assembly=MyProjectLibrary"
         x:Name="baseViewport"
         x:Class="Main.Client.MyProject.Implementation.UIs.StandardViewports.Viewports.Viewport"
         MouseEnter="baseViewport_MouseEnter"
         mc:Ignorable="d"
         d:DesignHeight="300" d:DesignWidth="300" Loaded="BaseViewport_Loaded">
<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/MyLibs;component/ResourceDictionnary/ResourceDictionnary.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <Main_Client_MyProject_Library_Converters:BooleanToVisibilityCollapsedConverter x:Key="BooleanToVisibilityCollapsedConverter" />
        <Main_Client_MyProject_Library_Converters:BooleanToVisibilityCollapsedConverter x:Key="BooleanToVisibilityTestConverter" />
    </ResourceDictionary>
</UserControl.Resources>
<UserControl.CommandBindings>
    ...
</UserControl.CommandBindings>
<Grid>
    <GMap_NET_WindowsPresentation:GMapControl
        x:Name="gMapControl"
        MaxZoom="18"
        MinZoom="1"
        MouseDown="gMapControl_MouseDown"
        OnMapZoomChanged="gMapControl_OnMapZoomChanged"
        OnCurrentPositionChanged="gMapControl_OnCurrentPositionChanged"
        MouseMove="gMapControl_MouseMove"
        Loaded="gMapControl_Loaded"
        Drop="gMapControl_Drop"
        AllowDrop="True"
        IsEnabled="{Binding IsEnabled, ElementName=baseViewport}" MapType="OpenStreetMap">
        <GMap_NET_WindowsPresentation:GMapControl.ContextMenu>
            <ContextMenu Opened="ContextMenu_Opened">
                <MenuItem
                    Header="{x:Static Internationalization:Resources.VIEWPORT_ADDOBJECT}"
                    Command="{x:Static local:Viewport.CreateGraphicObjectRequestCommand}"
                    CommandTarget="{Binding Path=PlacementTarget,RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                    Visibility="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled, Converter={StaticResource BooleanToVisibilityTestConverter}}">
                    <MenuItem.Icon>
                        <Image Source="{DynamicResource EditIcon}" Width="32" Height="32" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="{x:Static Internationalization:Resources.VIEWPORT_OPTIONS}"
                         Visibility="{Binding Path=PlacementTarget.DataContext.MainOptionsVisibility, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Converter={StaticResource BooleanToVisibilityTestConverter}}" >
                    <MenuItem Header="{x:Static Internationalization:Resources.VIEWPORT_LOCKSUPERIORLEFTCORNER}" Command="{x:Static local:Viewport.LockSuperiorLeftCornerRequestCommand}" CommandTarget="{Binding Path=PlacementTarget,RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}">

                    </MenuItem>
                    <MenuItem Header="{x:Static Internationalization:Resources.VIEWPORT_LOCKINFERIORRIGHTCORNER}" Command="{x:Static local:Viewport.LockInferiorRightCornerRequestCommand}" CommandTarget="{Binding Path=PlacementTarget,RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}">

                    </MenuItem>
                    <MenuItem Header="{x:Static Internationalization:Resources.VIEWPORT_LOCKZOOMMAXONMAP}" Command="{x:Static local:Viewport.LockMaxZoomLevelRequestCommand}" CommandTarget="{Binding Path=PlacementTarget,RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}" >
                    </MenuItem>
                    <MenuItem Header="{x:Static Internationalization:Resources.VIEWPORT_LOCKZOOMMINONMAP}" Command="{x:Static local:Viewport.LockMinZoomLevelRequestCommand}" CommandTarget="{Binding Path=PlacementTarget,RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}" >
                    </MenuItem>
                </MenuItem>

            </ContextMenu>
        </GMap_NET_WindowsPresentation:GMapControl.ContextMenu>
    </GMap_NET_WindowsPresentation:GMapControl>
</Grid>

The converter was changed for both menuItems for tests. It is never called.

Upvotes: 0

Views: 594

Answers (1)

Florian Gl
Florian Gl

Reputation: 6014

The problem is your MenuItem has no property which is called MainOptionsVisibility, only your Window has. Through RelativeSource={RelativeSource Self} your Binding to the MenuItem.

RelativeSource={RelativeSource AncestorType={x:Type Window}} wont work either, because your ContextMenu isnt part of the Windows logical or visual Tree.

What you could do is set the DataContext of the ContextMenus PlacementTarget (GMap_NET_WindowsPresentation:GMapControl) or one of its ancestors to your Window (e.g. through setting the Windows DataContext to itself (<Window ...DataContext={Binding RelativeSource={RelativeSource Self}}.../>) and the do something like:

<MenuItem Header="{x:Static Internationalization:Resources.OPTIONS}" Visibility="{Binding Path=PlacementTarget.DataContext.MainOptionsVisibility, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Converter={StaticResource BooleanToVisibilityCollapsedConverter}}" >

Upvotes: 1

Related Questions