0

I have a TextBlock and want to show information from the database. I wan't to display the text like this:

Task for Day xy
- blabla
- blabla 
- more blablabla

Somehow I need to include a character for the newlines.. I thoug about \n.. so the text in the database would look like:

Task for Day xy\n- blabla\n- blabla\n- more blablabla

doesn't work.

I tried and searched online for hours.. no solution how to do that. Also this doesn't work:

Task for Day xy<LineBreak />- blabla<LineBreak />- blabla<LineBreak />- more blablabla

the application shows exactly that string from the database without new lines..

here the part of the xaml:

<Grid>
  <Grid.ColumnDefinitions>
      <ColumnDefinition Width="100" />
      <ColumnDefinition Width="*" />
  </Grid.ColumnDefinitions>
  <TextBlock Grid.Column="1" Text="{Binding Date, StringFormat={}{0:dd.MM.yyyy}}" Margin="6" />
  <TextBlock Grid.Column="2" Text="{Binding Notes}" Margin="6" />
</Grid>

any ideas?

pat
  • 216
  • 2
  • 7

2 Answers2

2

I would not recommend storing the line breaks in the database, as line breaks will not be the same across platforms. I would just add the line breaks before displaying using System.Environment.NewLine.

Brent Stewart
  • 1,830
  • 14
  • 21
0

Just include a regular newline character in the text, no fancy escape sequences or XML tags:

 string fromDb = ... // get string from DB
 myPropertyWhichIsBoundToWpf = fromDb.Replace("hereShouldBeALinebreak", 
                                              System.Environment.NewLine);
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • thank you, I thougt about something like this. just hoped there was a better solution – pat Aug 19 '11 at 06:00
  • @patricci: An alternative would be to create a Converter that adds the newline for you. That way, you can bind the data directly without adding a new property: http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx – Heinzi Aug 19 '11 at 06:05