3

我有一个 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));
}
4

1 回答 1

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);
}
于 2012-04-23T22:13:01.007 回答