David Schmitt
David Schmitt

Reputation: 59346

Workaround jarring menu-in-toolbar style in WPF

I've troubles getting to play a <Menu/> within a <Toolbar/> to play nice. My Example, as follows is placed in an empty window in a default WpfApplication project template from VS2010:

<DockPanel>
    <ToolBar DockPanel.Dock="Top">
        <Button Content="Button 1"/>
        <Button Content="Button 2"/>
        <Separator/>
        <CheckBox Content="CheckBox 1"/>
        <CheckBox Content="CheckBox 2"/>
        <Separator/>
        <Menu>
            <MenuItem Header="Menu">
                <MenuItem Header="File">
                    <MenuItem Header="Copy"/>
                    <MenuItem Header="Paste"/>
                </MenuItem>
            </MenuItem>
        </Menu>
    </ToolBar>
    <Menu DockPanel.Dock="Top">
        <MenuItem Header="Menu">
            <MenuItem Header="File">
                <MenuItem Header="Copy"/>
                <MenuItem Header="Paste"/>
            </MenuItem>
        </MenuItem>
    </Menu>
    <Grid>

    </Grid>
</DockPanel>

The default style mis-aligns both the label and the hover-indicator:

menu-in-toolbar styling fail

I'm no designer, nor a WPF-guru, so my simple attempts at getting the menu's style closer to the buttons have failed catastrophically.

Has anybody a simple (ideally based on Toolbar.MenuStyleKey) Style to get the Menu back in line?

Upvotes: 3

Views: 874

Answers (1)

Anvaka
Anvaka

Reputation: 15823

Here is a quick thing that seemed to work in KaXaml:

    <ToolBar DockPanel.Dock="Top">
        <Button Content="Button 1"/>
        <Button Content="Button 2"/>
        <Separator/>
        <CheckBox Content="CheckBox 1"/>
        <CheckBox Content="CheckBox 2"/>
        <Separator/>
        <Menu Margin="0, -1, 0, 0" Style="{x:Null}">
            <MenuItem Header="Menu">
                <MenuItem Header="File">
                    <MenuItem Header="Copy"/>
                    <MenuItem Header="Paste"/>
                </MenuItem>
            </MenuItem>
        </Menu>
    </ToolBar>

Upvotes: 1

Related Questions