Adding a linebreak/new line to a WPF Wrap Panel
Asked Answered
L

2

16

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

Leatherneck answered 27/8, 2010 at 18:30 Comment(2)
A wrap panel holds UI elements, and you can add your own custom controls anywhere. Please clarify what you are trying to accomplish and where you are encountering difficulty.Pelting
@Jay- I want a line break in a WPF Wrap panel. There's my difficulty and what I am trying to accomplish.Leatherneck
P
18

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.

Pelting answered 27/8, 2010 at 18:42 Comment(2)
Yes, your update is exactly what I'm thinking! The only problem I have now is that I am adding element programatically like this: wrapPanel.childern.add(element). However, if I create a new stackpanel for every line break, I have to change wrapPanel/stackPanel to stackPanel2. Is there a way to add elements to the newest panel?Leatherneck
@DMan WrapPanel latestPanel = new WrapPanel(); Every time you create a new StackPanel or WrapPanel, assign it to latestPanel, and then add children to it.Pelting
J
27
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>
Judicator answered 30/9, 2014 at 13:30 Comment(4)
It's always better to include at least a brief explanation of code in answers.Emprise
@PeterJ, but here it looks straight forward without explanationWinou
This really should be the accepted answer. Does exactly what the OP wanted. Jay's solution is just a workaround.Quinate
Ingenious! This is not limited to text use.Fraudulent
P
18

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.

Pelting answered 27/8, 2010 at 18:42 Comment(2)
Yes, your update is exactly what I'm thinking! The only problem I have now is that I am adding element programatically like this: wrapPanel.childern.add(element). However, if I create a new stackpanel for every line break, I have to change wrapPanel/stackPanel to stackPanel2. Is there a way to add elements to the newest panel?Leatherneck
@DMan WrapPanel latestPanel = new WrapPanel(); Every time you create a new StackPanel or WrapPanel, assign it to latestPanel, and then add children to it.Pelting

© 2022 - 2024 — McMap. All rights reserved.