我有一个绑定(bind)到对象集合的 ListView 。其中一个属性是名为 startDate 的 DateTime 对象。它以标准的 1/1/2001 1:00:00 PM 格式显示
我想将日期设置为 yyyy-MM-dd HH:mm:ss.fff 格式,仅用于显示目的。有没有办法在以上面所需的格式显示底层 DateTime 对象的同时保持其完好无损?我更愿意在 XAML 中执行此操作,而不是向对象添加显示属性或类似的东西。
如果重要的话,对象实现 INotifyPropertyChanged 接口(interface)。
<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>
在此先感谢大家。
最佳答案
只需更改绑定(bind)中的 StringFormat。
DisplayMemberBinding="{Binding Path=startDate, StringFormat='yyyy-MM-dd HH:mm:ss.fff'}"
关于c# - WPF/C# - 将日期格式应用于 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6984919/