XAML-在右键单击的元素顶部打开上下文菜单
我试图在右键单击的元素顶部打开上下文菜单。但它仅在鼠标单击的位置打开XAML-在右键单击的元素顶部打开上下文菜单,xaml,contextmenu,placement,Xaml,Contextmenu,Placement,我试图在右键单击的元素顶部打开上下文菜单。但它仅在鼠标单击的位置打开 <TextBlock Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" x:Name="testeTB" Text="Name:"> <TextBlock.ContextMenu> <ContextMenu> <ContextMe
<TextBlock Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" x:Name="testeTB" Text="Name:">
<TextBlock.ContextMenu>
<ContextMenu>
<ContextMenu.Style>
<Style TargetType="ContextMenu">
<Setter Property="Placement" Value="Top"></Setter>
<Setter Property="PlacementTarget" Value="{Binding ElementName=testeTB}"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContextMenu">
<controls:PopupMenu>
<controls:PopupMenu.Children>
<wpf:ArbeitListBoxItem Content="Add Messages"></wpf:ArbeitListBoxItem>
<wpf:ArbeitListBoxItem Content="Remove Messages"></wpf:ArbeitListBoxItem>
</controls:PopupMenu.Children>
</controls:PopupMenu>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ContextMenu.Style>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
正如您在我的示例中所看到的,我尝试将Placement设置为Top和and
也可以使用textblock元素名设置PlacementTarget,但是
没用
是否可以使用ContextMenu执行此操作?或者我应该使用弹出窗口?我更喜欢使用ContextMenu来设置目标控件的ContextMenuService.Placement属性,下面是一个XAML示例:
<Grid>
<TextBlock
Padding="10 5"
Background="Lime"
ContextMenuService.Placement="Right"
Grid.Row="1"
Grid.Column="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="Name:">
<TextBlock.ContextMenu>
<ContextMenu>
<ContextMenu.Template>
<ControlTemplate>
<Border Background="Red">
<TextBlock Text="Context menu content..." Margin="20 15" />
</Border>
</ControlTemplate>
</ContextMenu.Template>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</Grid>
谢谢刘,就这样。我必须在目标元素中设置。为了添加额外的信息以防有人需要它,我必须在我的PopupMenu用户控件的代码中设置它,因为它是供不同元素使用的通用PopupMenu。因此,我需要在我的示例中找到父元素TextBlock来设置其Placement属性。我使用了以下代码:FrameworkElementSystem.Windows.Controls.ContextMenuthis.VisualParent.PlacementTarget.ContextMenu.SetValueContextMenuersvice.PlacementProperty,System.Windows.Controls.Primitives.PlacementMode.Top;