简体   繁体   English

相对于按钮放置上下文菜单

[英]Placing a Context Menu relative to button

I'm trying to do exactly what is described here , except with a Rectangle instead of a Button. 我正在尝试完全执行此处描述的操作,除了使用Rectangle而不是Button之外。 I did what is described in the answer (among many other things), and I cannot get it to display as shown relative to the rectangle (which, in my case is being used by a button). 我做了答案中描述的事情(还有很多其他事情),但我无法使其显示为相对于矩形所示的形状(在我的情况下,该矩形被按钮使用)。

I am using a rectangle as a button because I didn't like the mouseover effects of a button and it seemed easier to just use a rectangle rather than building a custom button. 我将矩形用作按钮,是因为我不喜欢按钮的鼠标悬停效果,并且仅使用矩形而不是构建自定义按钮似乎更容易。 As a note, I had this same problem even when I was using a button, in Expression Blend 4. 请注意,即使在使用Expression Blend 4中的按钮时,我也遇到相同的问题。

This is the XAML for my rectangle: 这是我的矩形的XAML:

        <Rectangle x:Name="rectangle" HorizontalAlignment="Left" MouseDown="MenuClicked" Height="55" VerticalAlignment="Top" Width="135" Grid.ColumnSpan="2" Margin="0,1,0,0" ContextMenuService.Placement="Bottom">
        <Rectangle.ContextMenu>
            <ContextMenu x:Name="Menu" >

                <MenuItem x:Name="LoadXML" Header="Load XML" Command="{Binding Command}" CommandParameter="BrowseXML"/>
                <MenuItem x:Name="SaveXML" Header="Save XML" Command="{Binding Command}" CommandParameter="SaveXML"/>
                <MenuItem x:Name="SaveBinary" Header="Save Binary" Command="{Binding Command}" CommandParameter="SaveBinary"/>
                <MenuItem x:Name="SaveText" Header="Save Text" Command="{Binding Command}" CommandParameter="SaveText"/>
            </ContextMenu>
        </Rectangle.ContextMenu>

        <Rectangle.Fill>
            <ImageBrush ImageSource="Resources/Logo.png" Stretch="Uniform" />
        </Rectangle.Fill>

    </Rectangle>

This still displays the contextmenu directly next to the point of mouse click. 这仍然会在鼠标单击旁边直接显示上下文菜单。 If I set Placement to bottom within the Context Menu code, rather than adding the ContextMenuService bit to the Rectangle, it displays it at the bottom or top relative to the entire window. 如果我在上下文菜单代码中将Placement设置为底部,而不是将ContextMenuService位添加到Rectangle中,则将其显示在整个窗口的底部或顶部。

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

EDIT: I am also using a method to enable left-clicking of the rectangle. 编辑:我还使用一种方法来启用矩形的左键单击。 On MouseDown, it calls this method: 在MouseDown上,它调用此方法:

    private void MenuClicked(object sender, System.Windows.RoutedEventArgs e)
    {
        Menu.PlacementTarget = this;
        Menu.IsOpen = true;
    }

You need to set the ContextMenuService.PlacementTarget property so the framework knows which element the context menu should be positioned relative to, eg: 您需要设置ContextMenuService.PlacementTarget属性,以便框架知道上下文菜单应相对于哪个元素放置,例如:

<Rectangle x:Name="rectangle" ContextMenuService.PlacementTarget="{Binding RelativeSource={RelativeSource Self}}" ... />

Just add that property to your Rectangle declaration. 只需将该属性添加到您的Rectangle声明中即可。 When combined with ContextMenuService.Placement="Bottom" , the context menu will appear at the bottom as you intended. ContextMenuService.Placement="Bottom"结合使用时,上下文菜单将按预期显示在底部。

EDIT: Since you are opening the menu programatically, you need to set the Placement and PlacementTarget properties directly on the menu itself. 编辑:由于您是以编程方式打开菜单,因此需要直接在菜单本身上设置PlacementPlacementTarget属性。 The attached properties from ContextMenuService , when set on the menu's owner, will only take effect if the context menu is opened by conventional means, eg, a right click. 如果在菜单所有者上设置了ContextMenuService的附加属性,则只有在通过常规方式(例如,右键单击)打开上下文菜单时,该属性才会生效。 In your MouseDown handler, you are setting PlacementTarget = this; 在MouseDown处理程序中,您正在设置PlacementTarget = this; , but you should be setting PlacementTarget = rectangle; ,但您应该设置PlacementTarget = rectangle; .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM