0

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 setsz = 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.

Jackie Jones
  • 100
  • 1
  • 12
  • I'm not sure if I understand you correctly... lets say you have two text items to be displayed in a wrap panel. There are some possible cases: (1) both fit into the same line (2) both fit into separate lines (3) item 1 requires more width than available (4) item 2 requires more width than available (5) both items each require more width than available. Could you explain in detail, how you expect the resulting text layout in each case (specially 3, 4, 5)? – grek40 Dec 01 '17 at 16:05
  • @grek40 The way the WrapPanel is beign used is a bit odd but it is basically inserting 1 letter at a time as a new control each. I want to be able to see when in the sentence there is a '\n' character and then add a linebreak so a word doesn't have 1 letter on the top line and continues on the next line. – Jackie Jones Dec 01 '17 at 16:10
  • I don't have an immediate idea for a solution and honestly, I feel like this would be solved differently if you focus on your requirement rather than your hacky solution attempt ;) – grek40 Dec 01 '17 at 16:13
  • Basically, first explain why `My Text...` doesn't cover your requirements – grek40 Dec 01 '17 at 16:23
  • The controls added to the wrap panel are actually grids where each grid is a letter. The only reason there is the TextBlock check is since it doesn't (and will not) exist in the wrap panel. – Jackie Jones Dec 01 '17 at 16:26
  • 1
    That doesn't explain, *__why__* the grid and all the other stuff is needed to display a letter. You can't justify part A of your current approach with part B of your current approach if the whole approach is questioned :) – grek40 Dec 01 '17 at 16:30
  • Well, the project is more or less just me messing around and I am kind of creating my own "pixel" letters. I know I could download a free custom font or find one but I'm just experimenting with various things in all honesty. – Jackie Jones Dec 01 '17 at 16:42

0 Answers0