0

我想隐藏有关用户权限的 menuItem。菜单项被放置到用户控件中的上下文菜单(通过右键单击显示)中。权限通过主窗口传递给用户控件。我有一个错误 40:绑定错误。VS 找不到我在 xaml 文档中声明的属性。

主窗口.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>

并且 MainOptionsVisibility 在代码隐藏中被很好地声明为依赖属性。我已经检查过了,它已经很好地初始化了。其他项目的可见性还可以(我没有这样做)。

编辑:弗洛回答后的新 XAML 代码:

<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>

用于测试的两个 menuItems 的转换器都已更改。它永远不会被调用。

4

1 回答 1

1

问题是您的 MenuItem 没有称为的属性MainOptionsVisibility,只有您的 Window 有。通过RelativeSource={RelativeSource Self}您绑定到 MenuItem。

RelativeSource={RelativeSource AncestorType={x:Type Window}}也不起作用,因为您的 ContextMenu 不是 Windows 逻辑或可视树的一部分。

您可以做的是将 ContextMenus PlacementTarget ( GMap_NET_WindowsPresentation:GMapControl) 的 DataContext 或其祖先之一设置为您的 Window(例如,通过将 Windows DataContext 设置为自身(<Window ...DataContext={Binding RelativeSource={RelativeSource Self}}.../>)并执行以下操作:

<MenuItem Header="{x:Static Internationalization:Resources.OPTIONS}" Visibility="{Binding Path=PlacementTarget.DataContext.MainOptionsVisibility, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Converter={StaticResource BooleanToVisibilityCollapsedConverter}}" >
于 2012-10-18T06:34:15.750 回答