Andrew
Andrew

Reputation: 5093

WPF Menu: Wrap items

I'm working on a WPF control that gets placed inside a TabControl in another window, and I have a menu that stretches across the top of my custom control with the letters of the alphabet as such (for indexing purposes):

<UserControl x:Class="thispageclass"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d">
    <Grid>
        <StackPanel Orientation="Vertical">
            <Menu Name="mnu">
                <MenuItem Header="A" />
                <MenuItem Header="B" />
                <MenuItem Header="C" />
                ...
                <MenuItem Header="Y" />
                <MenuItem Header="Z" />
            </Menu>

            [other elements]
        </StackPanel>
    </Grid>
</UserControl>

If all the letters are present, the menu gets wider than the window so you can't see the last few items without making the window wider. It seems like it should be trivial to make the menu wrap around if the window is too small but I can't seem to figure out how.

Upvotes: 1

Views: 2497

Answers (2)

Andrew
Andrew

Reputation: 5093

I created my own minimal sample which worked, leading me to figuring out it was my custom styles that were messing up my menu.

Upvotes: 0

biju
biju

Reputation: 18030

Just override the ItemsPanel of your Menu to a WrapPanel

Sample

    <Menu.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel/>
        </ItemsPanelTemplate>
    </Menu.ItemsPanel>

Upvotes: 3

Related Questions