Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add newline to Stringformat in XAML

I'm binding the columnheaders of a datagrid to an observablecollection of dates, to display the day and date in the columnheader. This works fine. However, I would like to add a newline or break using the string. How can this be done?

<DataGridTextColumn>
    <DataGridTextColumn.HeaderTemplate>
        <DataTemplate>
            <TextBlock TextWrapping="Wrap" Text="{Binding DataContext.Week.Days[1].Date, StringFormat=ddd dd.MM.yyyy, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>

This displays the following Text: Tue 06.12.2016 What I want it to display is

Tue
06.12.2016

like image 991
Mister 832 Avatar asked Nov 30 '22 15:11

Mister 832


2 Answers

For folks looking to put a newline in StringFormat (or ContentStringFormat, if you need to use Label), this answer has a way. You can use:

HEX represenation of CR/LF &#x0d;&#x0a; (or just line feed &#x0a;):

So, for the code in question:

StringFormat=ddd&#x0d;&#x0a;dd.MM.yyyy
like image 185
Vimes Avatar answered Dec 20 '22 04:12

Vimes


Set the TextBlock's Inlines property:

<TextBlock DataContext="{Binding DataContext.Week.Days[1].Date,
                         RelativeSource={RelativeSource AncestorType=DataGrid}}">
    <Run Text="{Binding Mode=OneWay, StringFormat=ddd}"/>
    <LineBreak/>
    <Run Text="{Binding Mode=OneWay, StringFormat=dd.MM.yyyy}"/>
</TextBlock>
like image 40
Clemens Avatar answered Dec 20 '22 03:12

Clemens