我想隐藏一个关于用户权限的menuItem。菜单项放入上下文菜单(右键单击显示)到userControl中。权限通过主窗口传递给用户控件。我有一个错误40:绑定错误。 VS无法在xaml文档中找到我声明的属性。
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>
MainOptionsVisibility在代码隐藏中被声明为依赖属性。我已经检查过,它已初步完成。其他项目的可见性是可以的(我没有这样做)。
编辑:Flo回答后的新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更改了转换器以进行测试。永远不会被称为。
答案 0 :(得分: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}}.../
&gt;)和做类似的事情:
<MenuItem Header="{x:Static Internationalization:Resources.OPTIONS}" Visibility="{Binding Path=PlacementTarget.DataContext.MainOptionsVisibility, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Converter={StaticResource BooleanToVisibilityCollapsedConverter}}" >