DateSelectorButton.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Avalonia;
  2. using Avalonia.Controls.Primitives;
  3. using Avalonia.Data;
  4. using Avalonia.Data.Converters;
  5. using CommunityToolkit.Mvvm.Input;
  6. using InABox.Avalonia.Components.DateSelector;
  7. using InABox.Core;
  8. using System.Globalization;
  9. namespace InABox.Avalonia.Components;
  10. public class DateSelectorDateChangedEventArgs(DateTime? oldDate, DateTime? newDate)
  11. {
  12. public DateTime? OldDate { get; set; } = oldDate;
  13. public DateTime? NewDate { get; set; } = newDate;
  14. }
  15. public partial class DateSelectorButton : TemplatedControl
  16. {
  17. public static readonly StyledProperty<string> PromptProperty =
  18. AvaloniaProperty.Register<DateSelectorButton, string>(nameof(Prompt));
  19. public static readonly StyledProperty<string> PrefixProperty =
  20. AvaloniaProperty.Register<DateSelectorButton, string>(nameof(Prefix));
  21. public static readonly StyledProperty<string> FormatProperty =
  22. AvaloniaProperty.Register<DateSelectorButton, string>(nameof(Format));
  23. public static readonly StyledProperty<DateTime?> DateProperty =
  24. AvaloniaProperty.Register<DateSelectorButton, DateTime?>(nameof(Date), defaultBindingMode: BindingMode.TwoWay);
  25. public string Prompt
  26. {
  27. get => GetValue(PromptProperty);
  28. set => SetValue(PromptProperty, value);
  29. }
  30. public string Prefix
  31. {
  32. get => GetValue(PrefixProperty);
  33. set => SetValue(PrefixProperty, value);
  34. }
  35. public string Format
  36. {
  37. get => GetValue(FormatProperty);
  38. set => SetValue(FormatProperty, value);
  39. }
  40. public DateTime? Date
  41. {
  42. get => GetValue(DateProperty);
  43. set => SetValue(DateProperty, value);
  44. }
  45. public event EventHandler<DateSelectorDateChangedEventArgs>? DateChanged;
  46. static DateSelectorButton()
  47. {
  48. DateProperty.Changed.AddClassHandler<DateSelectorButton>(DateProperty_Changed);
  49. }
  50. private static void DateProperty_Changed(DateSelectorButton button, AvaloniaPropertyChangedEventArgs args)
  51. {
  52. button.DateChanged?.Invoke(button, new(
  53. args.OldValue is DateTime oldDate ? oldDate : null,
  54. args.NewValue is DateTime newDate ? newDate : null));
  55. }
  56. [RelayCommand]
  57. private async Task Click()
  58. {
  59. var date = await Navigation.Popup<DateSelectorViewModel, DateTime?>(x =>
  60. {
  61. x.Date = Date;
  62. });
  63. if (date.HasValue)
  64. {
  65. Date = date.Value == DateTime.MinValue ? null : date.Value;
  66. }
  67. }
  68. }
  69. public class DateSelectorDateTimeFormatter : IMultiValueConverter
  70. {
  71. public object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
  72. {
  73. var date = values[0] as DateTime?;
  74. if (values[1] is not DateSelectorButton btn) return null;
  75. if (date.HasValue && !date.Value.IsEmpty())
  76. {
  77. var sFormat = btn.Format.NotWhiteSpaceOr("dd MMMM yy");
  78. var fmt = "{0} {1:" + sFormat + "}";
  79. return string.Format(fmt, btn.Prefix, date.Value).Trim();
  80. }
  81. return btn.Prompt.NotWhiteSpaceOr("Select Date");
  82. }
  83. }