Adding a linebreak/new line to a WPF Wrap Panel

Dominic K picture Dominic K · Aug 27, 2010 · Viewed 17.4k times · Source

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?)

Answer

Denis Shishkanov picture Denis Shishkanov · Sep 30, 2014
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>