DateConverter.cs 938 B

123456789101112131415161718192021222324252627282930
  1. using Avalonia.Data.Converters;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace AvaloniaApplication2.Resources
  9. {
  10. internal class DateConverter : IValueConverter
  11. {
  12. // from VM to Viev
  13. public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
  14. {
  15. if (value is DateTime) return new DateTimeOffset((DateTime)value, TimeSpan.Zero);
  16. return null;
  17. }
  18. //from View to VM
  19. public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
  20. {
  21. if (value is DateTimeOffset myDateTimeOffset)
  22. {
  23. return new DateTime(myDateTimeOffset.Year, myDateTimeOffset.Month, myDateTimeOffset.Day);
  24. }
  25. return null;
  26. }
  27. }
  28. }