using System.Globalization; using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Metadata; using Avalonia.Controls.Primitives; using Avalonia.Input; using Avalonia.Interactivity; using Irihi.Avalonia.Shared.Contracts; using Irihi.Avalonia.Shared.Helpers; namespace Ursa.Controls; [TemplatePart(PART_Button, typeof(Button))] [TemplatePart(PART_Popup, typeof(Popup))] [TemplatePart(PART_TextBox, typeof(TextBox))] [TemplatePart(PART_Calendar, typeof(CalendarView))] public class DatePicker: DatePickerBase, IClearControl { public const string PART_Button = "PART_Button"; public const string PART_Popup = "PART_Popup"; public const string PART_TextBox = "PART_TextBox"; public const string PART_Calendar = "PART_Calendar"; private Button? _button; private Popup? _popup; private TextBox? _textBox; private CalendarView? _calendar; public static readonly StyledProperty SelectedDateProperty = AvaloniaProperty.Register( nameof(SelectedDate)); public DateTime? SelectedDate { get => GetValue(SelectedDateProperty); set => SetValue(SelectedDateProperty, value); } public static readonly StyledProperty WatermarkProperty = AvaloniaProperty.Register( nameof(Watermark)); public string? Watermark { get => GetValue(WatermarkProperty); set => SetValue(WatermarkProperty, value); } static DatePicker() { SelectedDateProperty.Changed.AddClassHandler((picker, args) => picker.OnSelectionChanged(args)); } private void OnSelectionChanged(AvaloniaPropertyChangedEventArgs args) { if (args.NewValue.Value is null) { _calendar?.ClearSelection(); _textBox?.Clear(); } else { _calendar?.MarkDates(startDate: args.NewValue.Value, endDate: args.NewValue.Value); _textBox?.SetValue(TextBox.TextProperty, args.NewValue.Value.Value.ToString(DisplayFormat ?? "yyyy-MM-dd")); } } protected override void OnApplyTemplate(TemplateAppliedEventArgs e) { base.OnApplyTemplate(e); GotFocusEvent.RemoveHandler(OnTextBoxGetFocus, _textBox); TextBox.TextChangedEvent.RemoveHandler(OnTextChanged, _textBox); PointerPressedEvent.RemoveHandler(OnTextBoxPointerPressed, _textBox); Button.ClickEvent.RemoveHandler(OnButtonClick, _button); if (_calendar != null) { _calendar.OnDateSelected -= OnDateSelected; } _button = e.NameScope.Find