在win 8 metro app中打开上下文菜单

时间:2012-04-23 09:05:26

标签: windows-8

我有ListView中定义的项目列表。当用户点击或点击任何项目时,我想在所选项目的正上方显示一个PopupMenu。我该如何定位PopupMenu?

varmenu =newPopupMenu();
menu.Commands.Add(
newUICommand("Remove", (x) =>
{...
// Create the message dialog and set its content
}, 1));

var chosenCommand     =awaitmenu.ShowForSelectionAsync(GetElementRect((FrameworkElement)sender));

Rect GetElementRect(FrameworkElement element)
{    
    GeneralTransform buttonTransform = element.TransformToVisual(null);
    Pointpoint = buttonTransform.TransformPoint(newPoint());
    returnnewRect(point,newSize(element.ActualWidth, element.ActualHeight));
}

1 个答案:

答案 0 :(得分:3)

这是我用来打开应用栏中按钮上方弹出菜单的代码。

private async void OnOpenMenu(object sender, RoutedEventArgs e)
{
    var button = (Button)sender;
    var transform = button.TransformToVisual(this);
    var point = transform.TransformPoint(new Point(0, 0));

    var menu = new PopupMenu();

    menu.Commands.Add(new UICommand("Menu Item 1"));
    menu.Commands.Add(new UICommand("Menu Item 2"));

    await menu.ShowAsync(point);
}