WPF/C# - 将日期格式应用于listview

Yat*_*rix 16 c# data-binding wpf listview date-format

我有一个listview绑定到一组对象.其中一个属性是名为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>
Run Code Online (Sandbox Code Playgroud)

所有人都提前谢谢.

Ray*_*Ray 44

简单地更改绑定中的StringFormat.

DisplayMemberBinding="{Binding Path=startDate, StringFormat='yyyy-MM-dd HH:mm:ss.fff'}"
Run Code Online (Sandbox Code Playgroud)