单击“空白区域”时,上下文菜单不出现;在 StackPanel 中
我有一个 HierarchicalDataTemplate,它在水平 StackPanel 中包含一些简单的项目。 上下文菜单也分配给根 StackPanel 容器:
<HierarchicalDataTemplate DataType="{x:Type data:GroupViewModel}"
ItemsSource="{Binding Path=Children}">
<StackPanel Orientation="Horizontal" Margin="2" ContextMenuOpening="groupContextMenuOpening">
<StackPanel.ContextMenu>
<StaticResource ResourceKey="groupContextMenu" />
</StackPanel.ContextMenu>
<Rectangle Width="16" Height="15" Fill="{Binding Converter={StaticResource HierarchyLevelConverter}}" Margin="0 0 3 0" Cursor="Hand" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown" >
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding SetCurrent}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Rectangle>
<Image Width="16" Height="15" Source="{Binding Path=GroupState, Converter={StaticResource GroupStateConverter}}" Cursor="Hand" Margin="0 0 3 0">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown" >
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding ToggleGroupState}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Image>
</StackPanel>
</HierarchicalDataTemplate>
主机 TreeView 也使用 ContextMenu。 这个想法是,如果右键单击 TreeView 中的某个项目,您将获得它的上下文菜单。 如果您右键单击树视图中的空白区域,您将获得带有较少选项的“默认”上下文菜单。
除了一个奇怪的问题之外,这一切都有效。 正如您所看到的,水平 StackPanel 在图像之间留有边距。如果我右键单击项目之间的空间,则会显示 TreeView 上下文菜单。我希望当我右键单击 StackPanel 本身时,会弹出 HierarchicalDataTemplate 中定义的 ContextMenu。
我发现,如果我为 StackPanel 分配背景颜色,它就可以工作,但如果可能的话,我宁愿避免这种情况。
有什么想法吗?
I have a HierarchicalDataTemplate which has some simple Items contained in a horizontal StackPanel.
A context menu is assigned to the root StackPanel container as well:
<HierarchicalDataTemplate DataType="{x:Type data:GroupViewModel}"
ItemsSource="{Binding Path=Children}">
<StackPanel Orientation="Horizontal" Margin="2" ContextMenuOpening="groupContextMenuOpening">
<StackPanel.ContextMenu>
<StaticResource ResourceKey="groupContextMenu" />
</StackPanel.ContextMenu>
<Rectangle Width="16" Height="15" Fill="{Binding Converter={StaticResource HierarchyLevelConverter}}" Margin="0 0 3 0" Cursor="Hand" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown" >
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding SetCurrent}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Rectangle>
<Image Width="16" Height="15" Source="{Binding Path=GroupState, Converter={StaticResource GroupStateConverter}}" Cursor="Hand" Margin="0 0 3 0">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown" >
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding ToggleGroupState}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Image>
</StackPanel>
</HierarchicalDataTemplate>
The host TreeView also uses a ContextMenu.
The idea is that if you right click on an item in the TreeView, you will get it's ContextMenu.
If you right clik in empty space in the tree view you will get the 'default' context menu with lesser options.
This is all working expcept one odd problem.
As you can see the horizontal StackPanel leaves a margin between the images. If I right click in that space between items, the TreeView context menu shows up. I would expect the ContextMenu defined in my HierarchicalDataTemplate to pop up as i AM right click on the StackPanel itself.
I've found that if I assign a background color to the StackPanel it then works, but I would prefer to avoid this if possible.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
StackPanel
的Background
设置为Transparent
以使其在这些空白区域中进行测试。Set the
Background
of theStackPanel
toTransparent
to make it hit test in those empty areas.嵌入 < 可能是个好主意。画布>里面一个<滚动查看器> 。
然后,我将上述 @brunnerh 解决方案直接应用于画布。
有用。
It can be a good idea to embed the < Canvas > inside a < ScrollViewer > .
Then, I applied the above @brunnerh solution to the Canvas, directly.
It works.