Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a linebreak/new line to a WPF Wrap Panel

Tags:

wpf

wrappanel

Does anyone know if it's even possible to enter a line break into a WPF Wrap panel? It goes against what the wrap panel is for, so I'm not sure if it's possible.

And if it's not, is there any other WPF control that actually allows me to enter a line break into it and supports adding children (my own custom controls?)

like image 603
Dominic K Avatar asked Aug 27 '10 18:08

Dominic K


2 Answers

public class NewLine : FrameworkElement
{
    public NewLine()
    {
        Height = 0;
        var binding = new Binding
        {
            RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(WrapPanel), 1),
            Path = new PropertyPath("ActualWidth")
        };
        BindingOperations.SetBinding(this, WidthProperty, binding);
    }
}

<WrapPanel>
    <TextBox Text="Text1"/>
    <TextBox Text="Text2"/>
    <my:NewLine/>
    <TextBox Text="Text3"/>
    <TextBox Text="Text4"/>
</WrapPanel>
like image 126
Denis Shishkanov Avatar answered Oct 24 '22 07:10

Denis Shishkanov


This is a line break in a WrapPanel:

<WrapPanel>
    <TextBlock Text="&#xD;"/>
</WrapPanel>

Update

I think I figured out what you're trying to ask. If you have a WrapPanel that is laying out by row, and you want to force it to the next row, you can replace the single WrapPanel with

 <StackPanel Orientation="Vertical">
  <StackPanel Orientation="Horizontal">
   <!-- items -->
  </StackPanel>
  <StackPanel Orientation="Horizontal">
   <!-- items -->
  </StackPanel>
  <StackPanel Orientation="Horizontal">
   <!-- items -->
  </StackPanel>
 </StackPanel>

If you want to preserve wrapping of individual rows, you can use WrapPanels inside the vertical StackPanel.

like image 18
Jay Avatar answered Oct 24 '22 06:10

Jay