button - 单击按钮时添加下拉菜单 - Windows 8

标签 button drop-down-menu windows-8 microsoft-metro

我想在用户单击按钮时显示下拉菜单。类似 comboBox 但它不是组合框,而是一个按钮。我该怎么做??

最佳答案

我使用 PopupMenu 解决了这个问题。这是代码供其他人引用。

    public static Rect GetElementRect(FrameworkElement element)
    {
        GeneralTransform buttonTransform = element.TransformToVisual(null);
        Point point = buttonTransform.TransformPoint(new Point());
        return new Rect(point, new Size(element.ActualWidth, element.ActualHeight));
    }

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        var menu = new PopupMenu();
        menu.Commands.Add(new UICommand("Label", (command) =>
        {
            //do work
        }));

        // We don't want to obscure content, so pass in a rectangle representing the sender of the context menu event.
        // We registered command callbacks; no need to handle the menu completion event
        var chosenCommand = await menu.ShowForSelectionAsync(GetElementRect((FrameworkElement)sender));
        if (chosenCommand == null) // The command is null if no command was invoked.
        {

        }
    }

关于button - 单击按钮时添加下拉菜单 - Windows 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13455273/

相关文章:

ios - 如何将变量分配给按钮或从按钮调用变量

c# - 使用 C# 创建动态按钮并按预定义的顺序放置它们

Javascript下拉菜单onclick,(关闭其他菜单,当一个打开时)

javascript - Ajax 第二次不工作

c# - 在可移植类中实现基于事件的异步模式

windows-8 - 全文搜索 winrt(windows runtime、windows store app 等)

html - 为什么我不能使用 CSS 来移动按钮?

javascript - 我们可以通过javascript禁用浏览器的按钮(后退/前进/刷新)

javascript - 如何定义动态下拉菜单(示例)。?

javascript - Snap View 检查通过 javascript