WPF ContextMenuOpening 未在带有 StackPanel 的 TreeViewItem 上触发

问题描述 投票:0回答:1

TreeView
中,我有一个
HierarchicalDataTemplate
TreeViewItem
位于水平
StackPanel

当我右键单击

Image
TextBlock
时,会触发
ContextMenuOpening
事件,但是当我在图像和文本块之间(即两者之间的边距)右键单击时,会触发
ContextMenuOpening
事件没有被触发。我做错了什么,导致整个
ContextMenuOpening
上触发
StackPanel
事件?

<TreeView.Resources>
    <HierarchicalDataTemplate DataType="{x:Type treeViewVM:BaseTreeViewItemVM}" ItemsSource="{Binding SubItems}">
        <Grid ContextMenu="{Binding ContextMenu}">
            <StackPanel Orientation="Horizontal" Margin="2">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="PreviewMouseRightButtonDown">
                        <i:ChangePropertyAction PropertyName="IsSelected" Value="true" TargetObject="{Binding}" />
                    </i:EventTrigger>
                    <i:EventTrigger EventName="ContextMenuOpening">
                        <i:InvokeCommandAction Command="{Binding ContextMenuOpeningCommand}" PassEventArgsToCommand="True" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                <Image Stretch="None" Source="{Binding Icon}" />
                <TextBlock Text="{Binding Label}" Margin="5,0,0,0" />
            </StackPanel>
        </Grid>
    </HierarchicalDataTemplate>
</TreeView.Resources>

我尝试了其他两个版本但没有成功:

        <Grid ContextMenu="{Binding ContextMenu}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="PreviewMouseRightButtonDown">
                    <i:ChangePropertyAction PropertyName="IsSelected" Value="true" TargetObject="{Binding}" />
                </i:EventTrigger>
                <i:EventTrigger EventName="ContextMenuOpening">
                    <i:InvokeCommandAction Command="{Binding ContextMenuOpeningCommand}" PassEventArgsToCommand="True" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <StackPanel Orientation="Horizontal" Margin="2">
                <Image Stretch="None" Source="{Binding Icon}" />
                <TextBlock Text="{Binding Label}" Margin="5,0,0,0" />
            </StackPanel>
        </Grid>



        <StackPanel Orientation="Horizontal" Margin="2" ContextMenu="{Binding ContextMenu}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="PreviewMouseRightButtonDown">
                    <i:ChangePropertyAction PropertyName="IsSelected" Value="true" TargetObject="{Binding}" />
                </i:EventTrigger>
                <i:EventTrigger EventName="ContextMenuOpening">
                    <i:InvokeCommandAction Command="{Binding ContextMenuOpeningCommand}" PassEventArgsToCommand="True" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <Image Stretch="None" Source="{Binding Icon}" />
            <TextBlock Text="{Binding Label}" Margin="5,0,0,0" />
        </StackPanel>
wpf mvvm contextmenu stackpanel treeviewitem
1个回答
0
投票

您需要设置背景颜色。 null - 意味着没有背景并且点击“通过”。

<TreeView.Resources>
    <HierarchicalDataTemplate DataType="{x:Type treeViewVM:BaseTreeViewItemVM}" ItemsSource="{Binding SubItems}">
        <Grid ContextMenu="{Binding ContextMenu}"
              Background="Transparent">
© www.soinside.com 2019 - 2024. All rights reserved.