2

I want to display a drop down menu when a user clicks a button. Something like comboBox but instead of the comboBox its a button. How can I do it??

2 Answers 2

2

I solved it using PopupMenu. Here is the code for other's reference.

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

        }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Milan,

You'll need to create a custom control or a user control that combines a button and a popup. You could also just implement this in-place with a button and popup. I suggest you look at Callisto's Menu control and start from there to implement your dropdown menu: Callisto controls (includes a Menu)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.