Search code examples
wpfwrappanel

Aligning columns without WPF Grid


I am looking for a good way to design a multi-column layout which reflows the controls in the columns according to the space available. I have a list of labels and fields which display information, and sometimes the view they are contained in needs to be tall and skinny, other times short and wide.

A simple solution is to use a WrapPanel:

<WrapPanel Orientation="Vertical">
    <StackPanel Orientation="Horizontal">
        <Label>Some label:</Label>
        <TextBlock>Some value</TextBlock>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
        <Label>Some other label:</Label>
        <TextBlock>Some bigger value</TextBlock>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
        <Label>A:</Label>
        <TextBlock>B</TextBlock>
    </StackPanel>
</WrapPanel>

I want the labels and values all to line up horizontally into columns, without specifying a static width. Right now the Labels and TextBlocks are just sized based on their content.


Solution

  • Did you try to add WrapPanel as an ItemsContainer in ListBox?

    <ListBox>
      <ListBox.ItemsContainer>
        <WrapPanel />
      </ListBox.ItemsContainer>
    </ListBox>