wpf - add newline to wrappanel ?

  • Thread starter Thread starter Vivien Parlat
  • Start date Start date
V

Vivien Parlat

Hello,

I'm trying to create the equivalent of a textbox which could format
different kind of "values" using a string.
Currently i can do this: (myObject is an ItemsControl)

<myObject>
<myObject.Items>
The value of abc is:
<SomeHandler DataContext="{Binding abc}">
The value of def is:
<SomeHandler DataContext="{Binding def}">
<myObject.Items>
</myObject>

A TemplateSelector is used to transform each element into a TextBlock,
and a WrapPanel is used to layout the textblocks, so that resizing the
window keeps all textblocks displayed to user, like a the text in a
web browser.

My question is: how can I do to insert NewLines manually ? Web
browsers have an auto wrap feature, but they also include <br /> tags.
What's the easiest way to implement this ? And if it is not posible,
any idea i could do this with or without breaking my code ?

Thanks in advance.
 
Vivien Parlat said:
Hello,

I'm trying to create the equivalent of a textbox which could format
different kind of "values" using a string.
Currently i can do this: (myObject is an ItemsControl)

<myObject>
<myObject.Items>
The value of abc is:
<SomeHandler DataContext="{Binding abc}">
The value of def is:
<SomeHandler DataContext="{Binding def}">
<myObject.Items>
</myObject>

A TemplateSelector is used to transform each element into a TextBlock,
and a WrapPanel is used to layout the textblocks, so that resizing the
window keeps all textblocks displayed to user, like a the text in a
web browser.

My question is: how can I do to insert NewLines manually ? Web
browsers have an auto wrap feature, but they also include <br /> tags.
What's the easiest way to implement this ? And if it is not posible,
any idea i could do this with or without breaking my code ?

Thanks in advance.

If you just want to force a new line within a TextBlock you can do this:
<TextBlock>Line1<LineBreak />Line2</TextBlock>
Or, in code:
myTextBox.Text = "Line1" + Environment.NewLine + "Line 2";

However, if you want to force the WrapPanel to wrap after a particular text
block then I don't know of any official way to do it. One very nasty way
would be to insert something like the following where you want the wrap:
<TextBlock FontSize="0.01" Margin="0,0,100000000,0"/>
but I definitely do not recommend it!!!

Chris Jobson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top