Thought
Thought

Reputation: 5846

Split text according to size of stackpanel

On a windows store app project, i have 2 StackPanels one with 200x200 and another with 200x500 and a scrollviewer inside it(just the second one), i also have a string with some text.

what i want is to split the string so that i get 2 strings, and the first one fits on the 200x200 area, and the rest will go to the second stackapanel with the scrollviewer.

How can i split a string according to the dimensions?

i tried using some functions like this one but dindt quite get it.

private void textBox1_TextChanged(object sender, EventArgs e)
    {
        Size size = TextRenderer.MeasureText(textBox1.Text, textBox1.Font);
        textBox1.Width = size.Width;
        textBox1.Height = size.Height;
    }

Upvotes: 1

Views: 113

Answers (1)

Athari
Athari

Reputation: 34285

What you need is FormattedText class. TextRenderer is for WinForms, not WPF.

You can find sample implementation here: WPF equivalent to TextRenderer. You may need to asjust code to apply width and height of the box.

There're also methods for WinRT: How can I calculate the width of a string in Metro (without displaying it)?, but they depend on creating a TextBlock which isn't rendered.

Upvotes: 1

Related Questions