ToolStripDropDownButton equivalent in WPF?
Asked Answered
J

1

3

I'm very newbie to the WPF technology. I've been developing in WinForms for around 6 years.

Now I would like to advance one big step in my skills by learning WPF. I'm reading the introduction guides in MSDN, while as a personal exercise I'm trying to translate the sructure of a WinForms UI to its WPF UI equivalent.

This is a simple mockup of the WinForms controls structure that I would like to reproduce in WPF:

enter image description here

My problem is trying to reproduce the equivalent control/behavior for the ToolStripDropDownButton control.

In the Windows Forms Controls and Equivalent WPF Controls article, Microsoft just says that the equivalent control for a ToolStripDropDownButton is a ToolBar...with composition. I don't understand at all what it means about "with composition". I've been reading and experimenting with the Toolbar, but I didn't found the way to add a dropdown button thing inside the toolbar.

My question: someone could guide me or show me a brief example of how can I add a drop down button thing inside a Toolbar?.

Jolda answered 16/9, 2017 at 4:23 Comment(3)
Dropdown Menu in WPF Toolbar?Deontology
@Reza Aghaei Thanks for comment. My fear is that says it is a "menu", at first sight and talking from my ignorance it seems to be a menu that is shown when the button is clicked, this could mean I will lose/can not reproduce a button control that has by default an arrow drawn at the right of the button? (like in a ToolStripDropDownButton control), I'll try that solution to see if that "menu" at the end can have the expected behavior as a drop down button. Thanks again!Jolda
You're welcome. Based on your question in comments, I added a code sample containing arrow buttons as well as some other controls hosted in sub-menus.Deontology
D
5

You can put a Menu in Toolbar. A Menu contains some MenuItem. Each MenuItem has a Header which is it's content and can be a text or other elements. Also each MenuItem can have some nested or MenuItems. For example, to have a menu structure like this:

enter image description here

You can use such code:

<ToolBar>
    <Menu Background="#00000000">
        <MenuItem >
            <MenuItem.Header>
                <StackPanel Orientation="Horizontal">
                    <TextBlock>Menu 1</TextBlock>
                    <Path VerticalAlignment="Center" Margin="8,2,0,0"
                          Fill="Black" Data="M 0 0 L 3 3 L 6 0 Z"/>
                </StackPanel>
            </MenuItem.Header>
            <MenuItem Header="Menu 1-1" >
                <MenuItem Header="Menu 1-1-1"/>
                <MenuItem Header="Menu 1-1-2"/>
            </MenuItem>
            <MenuItem Header="Menu 1-2"/>
            <MenuItem>
                <MenuItem.Header><TextBox Width="100"/></MenuItem.Header>
            </MenuItem>
            <MenuItem>
                <MenuItem.Header><DatePicker Width="100"/></MenuItem.Header>
            </MenuItem>
        </MenuItem>
        <MenuItem >
            <MenuItem.Header>
                <StackPanel Orientation="Horizontal">
                    <TextBlock>Menu 2</TextBlock>
                    <Path VerticalAlignment="Center" Margin="8,2,0,0" 
                          Fill="Black" Data="M 0 0 L 3 3 L 6 0 Z"/>
                </StackPanel>
            </MenuItem.Header>
            <MenuItem Header="Menu 2-1"/>
            <MenuItem Header="Menu 2-2"/>
            <MenuItem Header="Menu 2-3"/>
        </MenuItem>
        <MenuItem Header="Menu 3"/>
    </Menu>
</ToolBar>
Deontology answered 17/9, 2017 at 20:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.