Override DateTime serialization to string
In my WPF app, I want to give the user the option to present all the dates in different time zones. That is, to keep the data as is but only present it differently in TextBlock Text Binding to DateTime .
I'm trying to achieve this without introducing a wrapping type or else use converters - this would be bad design as this could easily be missed by other developers who would be working on the code. Also, that way the existing code could be kept intact.
Is there a way to intervene in the DateTime serialization to string inside the TextBlock binding and insert my own logic there? using a custom serializer?
Or otherwise, is there a way for me to override the basic DateTime.ToString() Method and insert my own logic there?
you can use converter in binding:
https://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter(v=vs.110).aspx
IValueConverter
xaml:
“Source =”{Binding MyDate, Converter={StaticResource MyDateTimeZoneConverter}}
and add Timezoneconversion logic into class MyDateTimeZoneConverter:IValueConverter{...}
上一篇: C#日期时间为“YYYYMMDDHHMMSS”格式
下一篇: 将DateTime序列化重写为字符串