WPF/C# - Applying date format to listview

你离开我真会死。 提交于 2019-12-18 12:58:08

问题


I have a listview bound to a collection of objects. One of the properties is a DateTime object named startDate. It's displayed in the standard 1/1/2001 1:00:00 PM format

I want to put the date in yyyy-MM-dd HH:mm:ss.fff format just for display purposes. Is there a way to keep the underlying DateTime object in tact while displaying it in the desired format above? I'd prefer to do this in XAML as opposed to adding a display property to the object or something along those lines.

The objects implement the INotifyPropertyChanged interface, if that matters.

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

Thanks in advance, all.


回答1:


Simple change the StringFormat in your binding.

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


来源:https://stackoverflow.com/questions/6984919/wpf-c-applying-date-format-to-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!