简体   繁体   English

如何在自定义类中显示ContextMenu?

[英]How to show ContextMenu in custom class?

I am learning wpf technology. 我正在学习wpf技术。 I have created such custom class, which should react on mouse up event (should show popup menu) 我已经创建了这样的自定义类,该类应在发生鼠标向上事件时做出反应(应显示弹出菜单)

public class CustomControl : UIElement
{
    protected override void OnMouseUp(MouseButtonEventArgs args)    
    {
         ContextMenu context_menu = new ContextMenu();
         // How to show context_menu at specific point?
    }
};

I don't know how to show created context menu at speficic position. 我不知道如何在特定位置显示创建的上下文菜单。 It don't have specified Show method. 它没有指定Show方法。 UIElement don't have appropriated ContextMenu property UIElement没有适当的ContextMenu属性

Edit: I am not using XAML (I don't want create dependency between source code and design) 编辑:我不使用XAML(我不想在源代码和设计之间创建依赖项)

Using WPF you would design a context menu in XAML usually. 使用WPF,通常可以在XAML中设计上下文菜单。 A brief tutorial of how to do that is this for example. 例如,有关如何执行操作的简短教程。

Basically you write your context menu within the respective control and use Command s or mouse events like Click to assign a specific behaviour. 基本上,您可以在相应的控件内编写上下文菜单,并使用Command或鼠标事件(如Click来分配特定的行为。

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

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