在XAML中将换行符添加到Stringformat

Mis*_*832 2 data-binding wpf xaml string-formatting

我将数据网格的列标题绑定到日期的可观察集合,以在列标题中显示日期和日期。这很好。但是,我想添加一个换行符或使用字符串中断。如何才能做到这一点?

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

这将显示以下文本:Tue 06.12.2016我想要显示的是

周二
06.12.2016

Vim*_*mes 6

对于希望在StringFormat(或ContentStringFormat,如果需要使用Label)中放置换行符的人们,此答案有一种解决方法。您可以使用:

CR / LF的十六进制表示形式&#x0d;&#x0a;(或仅换行&#x0a;):

因此,对于有问题的代码:

StringFormat=ddd&#x0d;&#x0a;dd.MM.yyyy
Run Code Online (Sandbox Code Playgroud)


Cle*_*ens 5

设置 TextBlock 的Inlines属性:

<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>
Run Code Online (Sandbox Code Playgroud)