Button 单击按钮时添加下拉菜单-windows 8

Button 单击按钮时添加下拉菜单-windows 8,button,drop-down-menu,windows-8,microsoft-metro,Button,Drop Down Menu,Windows 8,Microsoft Metro,我想在用户单击按钮时显示下拉菜单。类似于组合框,但它不是组合框,而是一个按钮。我该怎么做呢?米兰 您需要创建一个自定义控件或一个用户控件,该控件结合了按钮和弹出窗口。您也可以通过一个按钮和弹出窗口就地实现这一点。我建议您看看Callisto的菜单控件,然后从那里开始实现您的下拉菜单: 我用弹出菜单解决了这个问题。这是代码,供他人参考 public static Rect GetElementRect(FrameworkElement element) { Gene

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

米兰

您需要创建一个自定义控件或一个用户控件,该控件结合了按钮和弹出窗口。您也可以通过一个按钮和弹出窗口就地实现这一点。我建议您看看Callisto的菜单控件,然后从那里开始实现您的下拉菜单:
我用弹出菜单解决了这个问题。这是代码,供他人参考

    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.
        {

        }
    }