【问题标题】:How to format TimeSpan in wpf (DataGrid)?如何在 wpf(DataGrid)中格式化 TimeSpan?
【发布时间】:2013-05-06 03:41:50
【问题描述】:

我有一个 TimeSpan 以刻度保存到 SqlCe 中,当我在 DataGrid 中加载数据时,我想在 HH:MM:SS 中格式化这个值。我试试这个:

<DataGridTextColumn Binding="{Binding tiempo, StringFormat={}{0:hh':'mm':'ss}, TargetNullValue=' --- '}"  Width="80" Header="Tiempo"/>

但 DataGrid 显示的是 'hh:mm:ss' 而不是值。

我也尝试使用其他模式,例如StringFormat="hh\:mm\:ss"

有什么想法吗?

谢谢!对不起我的英语不好!

【问题讨论】:

  • 不确定是否可以在绑定中使用String.Format。但是您可以通过使用ValueConverter 轻松实现此目的。看看这个tutorial
  • @programmer93 那是StringFormat 属性,而不是String.Format 方法。

标签: c# wpf datagrid format timestamp


【解决方案1】:

你可以用双反斜杠这样写:

<DataGridTextColumn Binding="{Binding tiempo, StringFormat=hh\\:mm\\:ss}"/>

或者像这样使用单个反斜杠:

<DataGridTextColumn>
    <DataGridTextColumn.Binding>
        <Binding Path="tiempo" StringFormat="hh\:mm\:ss"/>
    </DataGridTextColumn.Binding>
</DataGridTextColumn>

虽然这已经是默认格式,所以

<DataGridTextColumn Binding="{Binding tiempo}"/>

应该也可以。

有关更多示例,另请参阅 this answer

【讨论】:

  • mm 相同的结果,datagrid 显示 hh:mm:ss 但不显示格式化值
  • 我什至用 DataGridTextColumn 试过它,它对我有用。您能出示tiempo 属性的声明吗?
【解决方案2】:

我解决了这个问题!在数据库中,我像 bigint 一样存储“tiempo”,所以我将其更改为 nvarchar 并对其进行了一些修复。 谢谢大家!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 2019-10-23
    • 2013-04-12
    • 1970-01-01
    • 2014-12-25
    相关资源
    最近更新 更多