I'm trying to modify the solution found here to have a separate method that will insert a linebreak.
What I was able to do so far is very hackish but I basically inserted a control type that I don't use into the WrapPanel and from there I played around with the desired size values to trick the panel into thinking it was time to wrap the content.
protected override Size MeasureOverride(Size constraint)
{
Size curLineSize = new Size();
Size panelSize = new Size();
UIElementCollection children = base.InternalChildren;
for (int i = 0; i < children.Count; i++)
{
UIElement child = children[i] as UIElement;
// Flow passes its own constraint to children
child.Measure(constraint);
Size sz = child.DesiredSize;
if (child.GetType() == typeof(TextBlock))
{
sz.Width = constraint.Width + 1;
}
if (curLineSize.Width + sz.Width > constraint.Width) //need to switch to another line
{
panelSize.Width = Math.Max(curLineSize.Width, panelSize.Width);
panelSize.Height += curLineSize.Height;
curLineSize = child.DesiredSize;
if (sz.Width > constraint.Width) // if the element is wider then the constraint - give it a separate line
{
sz = child.DesiredSize;
panelSize.Width = Math.Max(sz.Width, panelSize.Width);
panelSize.Height += sz.Height;
curLineSize = new Size();
}
}
else //continue to accumulate a line
{
curLineSize.Width += sz.Width;
curLineSize.Height = Math.Max(sz.Height, curLineSize.Height);
}
}
// the last line size, if any need to be added
panelSize.Width = Math.Max(curLineSize.Width, panelSize.Width);
panelSize.Height += curLineSize.Height;
return panelSize;
}
Significant lines are if (child.GetType() == typeof(TextBLock))
, sz.Width = constraint.Width + 1;
and from there I changed the curLineSize = child.DesiredSize;
to 'curLineSize = child.DesiredSizeto keep in consistent with original code. Finally, I also set
sz = child.DesiredSize;` to keep that part consistent.
I know this is very hackish that's why I have been trying to create some sort of method that can be called and simply inserts a linebreak but I've had no luck.