23

is it possible to wrap content in a StackPanel?

I know that we can make use of a WrapPanel instead. But for code modifying reasons, I must make use of a StackPanel.

So, is there a way to make the items in a StackPanel wrap after say 5 items... Thanks!

Zam
  • 2,880
  • 1
  • 18
  • 33
user1202434
  • 2,243
  • 4
  • 36
  • 53
  • 3
    Why do you have to use a `StackPanel` instead of a `WrapPanel`? – Rachel Apr 13 '12 at 19:51
  • there are certain features of stack panel tht we have customized..so we have our own virtualizing supported by our stack panel..i dont want to come up reinventing the wheel for virtualizing a wrap panel.. – user1202434 Apr 14 '12 at 18:54

7 Answers7

32

For me, a simple WrapPanel works just fine:

<WrapPanel Orientation="Horizontal" Width="500" />

Not inside a StackPanel or any other container. And setting Width to a constant value can be superior im some cases, because binding it to ActualWidth can prevent down-sizing (e.g. when parent control is down-sized, WrapPanel is not)

bytecode77
  • 14,163
  • 30
  • 110
  • 141
17

Create nested StackPanels which contain the required number of items.

In the example below, you have two rows, respectively occupied by the <StackPanel Orientation="Horizontal"> elements, which in turn each contain five items that will be displayed horizontally next to each other.

<StackPanel Orientation="Vertical">
    <StackPanel Orientation="Horizontal">
        <Item1 />
        <Item2 />
        <Item3 />
        <Item4 />
        <Item5 />
    </StackPanel>
    <StackPanel Orientation="Horizontal">
        <Item1 />
        <Item2 />
        <Item3 />
        <Item4 />
        <Item5 />
    </StackPanel>
</StackPanel>
Douglas
  • 53,759
  • 13
  • 140
  • 188
9
<StackPanel>
        <StackPanel.Style>
            <Style TargetType="{x:Type StackPanel}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type StackPanel}">
                            <WrapPanel/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </StackPanel.Style>
    </StackPanel>
denis morozov
  • 6,236
  • 3
  • 30
  • 45
6

Depending on your scenario you could use a UniformGrid. A few example can also be found here.

You can define it to wrap after 5 Items like this.

<UniformGrid Columns="5">
 <Button />
 <Button />
 <Button />
</UniformGrid>

Each Item will, however get the exact same width, so not sure if this will work for you.

shriek
  • 5,157
  • 2
  • 36
  • 42
0

I don't think you can do it without a wrap panel. Maybe you can try putting a wrapPanel inside the stack panel - set its width to to the Actual width of the stack panel. You can bind it like Width="{Binding ActualWidth, ElementName=StackPanel1}"

But this will be just a hack - i think wrap panel is the best suited for your needs.

NoviceProgrammer
  • 3,347
  • 1
  • 22
  • 32
0

I put a stackpanel over the button. It won't affect button background. Then in the VB code I used Chr(12), to indicate a line feed:

Button1.Content = "first line" + Chr(12) + "second line"

You can add more lines using Chr(12).

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
0

For me it was the best and easiest solution. Set ItemsPanel property.

    <ItemsControl>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

    </ItemsControl>
atom
  • 1