Wpf 如何确定ContextMenu在哪一侧打开?

Wpf 如何确定ContextMenu在哪一侧打开?,wpf,Wpf,假设我得到了这个上下文菜单: <Style x:Key="{x:Type ContextMenu}" TargetType="{x:Type ContextMenu}"> <Setter Property="VerticalOffset" Value="-10"/> <Setter Property="HorizontalOffset" Value="-10"/> <Setter Property="Template">

假设我得到了这个上下文菜单:

<Style x:Key="{x:Type ContextMenu}" TargetType="{x:Type ContextMenu}">
    <Setter Property="VerticalOffset" Value="-10"/>
    <Setter Property="HorizontalOffset" Value="-10"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ContextMenu}">
                <Border Background="Transparent">
                    <Border.Effect>
                        <DropShadowEffect BlurRadius="10" ShadowDepth="0" Opacity="0.5"/>
                    </Border.Effect>
                    <Border Margin="10" Style="{StaticResource MenuBorderStyle}">
                        <Grid x:Name="SubMenu" Grid.IsSharedSizeScope="True">
                            <!-- StackPanel holds children of the menu. This is set by IsItemsHost=True -->
                            <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle"/>
                        </Grid>
                    </Border>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

如您所见,我将菜单的外观向上和向左偏移了10个像素,使
ContextMenu
的左上角位于光标的正下方

事实上,由于边框的边距为10像素,整个控件应该比光标更左上方,因为控件本身以阴影开始

而且,正如您所猜测的,当上下文菜单不出现在光标的右下角(例如光标靠近屏幕的底部或左侧)时,需要将偏移量更改为相反的值(通过X或Y或两个坐标)

问题是如何通过xaml做到这一点

(对不起我的英语)

尝试将属性设置为。也请签出,它可能会对您有所帮助。

尝试将属性设置为。还有,这可能会对你有所帮助