WPF button with drop down list and arrow

前端 未结 4 1920
生来不讨喜
生来不讨喜 2021-02-18 17:08

Can someone suggest the best way to have a button with an arrow and dropdown list like in visual studio toolbar button new item. As you can find in VS the mouse hover is highlig

相关标签:
4条回答
  • 2021-02-18 17:29

    The solution is to make use a menu item and decorate it.

    XAML Code:

    <MenuItem Click="AddPresetButton_Click" x:Name="AddPresetButton">
        <MenuItem.Icon>
            <Image Source="/MyApp.Application;component/Resources/add.png" Height="20"/>
        </MenuItem.Icon>
        <MenuItem.Header>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Add Preset"/>
                <Image Source="/MyApp.Application;component/Resources/arrow_down_simple.png"
                       Height="10" Margin="2,0,0,0"/>
            </StackPanel>
        </MenuItem.Header>
        <MenuItem.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Add 1"/>
                <MenuItem Header="Add 2"/>
                <MenuItem Header="Add 3"/>
            </ContextMenu>
        </MenuItem.ContextMenu>
    </MenuItem>
    

    C# Code: When the menu is pressed the context menu is opened.

    private void AddPresetButton_Click(object sender, RoutedEventArgs e)
    {
        var addButton = sender as FrameworkElement;
        if (addButton != null)
        {
            addButton.ContextMenu.IsOpen = true;
        }
    }
    
    0 讨论(0)
  • 2021-02-18 17:37

    Check out the ComboBox control that comes packaged with a standard WPF application.

    0 讨论(0)
  • 2021-02-18 17:43

    I would suggest to use WPF Tookit and its SplitButton control, its free

    0 讨论(0)
  • 2021-02-18 17:50

    It looks like you have three problems to solve:

    1. Styling / Layout
    2. Highlight dropdown and button OnMouseOver
    3. Change default button according to menu's last selection

    Styling / Layout

    Here are a couple of examples:

    • http://dvoituron.wordpress.com/2011/01/06/toolbar-dropdownbutton-in-wpf/
    • http://blogs.msdn.com/b/llobo/archive/2006/10/25/split-button-in-wpf.aspx

    I am sure there are many other ways (e.g. using a plain button and ComboBox styled appropriately)

    Highlighting dropdown and button OnMouseOver

    Experiment with triggers; e.g:

    • WPF Mouseover Trigger Effect for Child Controls
    • WPF - How to change children's style on mouseover of parent

    Change default button according to menu's last selection

    Try the MVVM approach: The button element will be bound to a property on your ViewModel. Each menu item will call an action (ICommand) in your ViewModel. This ViewModel will know which menu item was called, and update the button's property on the ViewModel. The button will automatically update using data binding.

    0 讨论(0)
提交回复
热议问题