【问题标题】:WPF/C# - Applying date format to listviewWPF/C# - 将日期格式应用于列表视图
【发布时间】:2011-08-08 15:59:33
【问题描述】:

我有一个绑定到对象集合的列表视图。其中一个属性是名为 startDate 的 DateTime 对象。它以标准的 1/1/2001 1:00:00 PM 格式显示

我想以 yyyy-MM-dd HH:mm:ss.fff 格式输入日期,仅用于显示目的。有没有办法在以上面所需的格式显示底层 DateTime 对象时保持其完整?我更喜欢在 XAML 中执行此操作,而不是向对象添加显示属性或类似的东西。

对象实现 INotifyPropertyChanged 接口,如果这很重要的话。

<ListView x:Name="lvBatches" 
                  SelectionMode="Single"
                  Margin="12,73,349,61" 
                  Background="WhiteSmoke" 
                  SelectionChanged="lvBatches_SelectionChanged"
                  ToolTip="Click on the column headers to sort by that column"
                  FontSize="10pt"
                  ItemContainerStyle="{StaticResource itemStyle}" 
                  ItemsSource="{Binding batchCollection}">
<!-- ... -->
    <GridViewColumn x:Name="colStart" 
                    Width="200" 
                    DisplayMemberBinding="{Binding startDate}">
        <GridViewColumnHeader Content="Start Date"
                              Click="GridViewColumnHeader_Click"/>
    </GridViewColumn>

提前谢谢大家。

【问题讨论】:

    标签: c# wpf data-binding listview date-format


    【解决方案1】:

    简单地更改绑定中的 StringFormat。

    DisplayMemberBinding="{Binding Path=startDate, StringFormat='yyyy-MM-dd HH:mm:ss.fff'}"
    

    【讨论】:

    • 天哪 - 我试过了,但我没有把字符串放在单引号中。菜鸟失误。谢谢,雷。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    • 2016-05-24
    • 2018-03-21
    • 2013-03-21
    • 2014-04-07
    相关资源
    最近更新 更多