Search code examples
wpfxamldata-bindingstring-formatting

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


Solution

  • 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>