fix: Fix DatePicker initialization issue.
This commit is contained in:
@@ -3,6 +3,8 @@
|
|||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
xmlns:u="https://irihi.tech/ursa"
|
xmlns:u="https://irihi.tech/ursa"
|
||||||
|
xmlns:viewModels="clr-namespace:Ursa.Demo.ViewModels"
|
||||||
|
x:DataType="viewModels:DatePickerDemoViewModel"
|
||||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||||
x:Class="Ursa.Demo.Pages.DatePickerDemo">
|
x:Class="Ursa.Demo.Pages.DatePickerDemo">
|
||||||
<StackPanel Margin="20" HorizontalAlignment="Left">
|
<StackPanel Margin="20" HorizontalAlignment="Left">
|
||||||
@@ -10,5 +12,9 @@
|
|||||||
<TextBlock Text="{Binding #singlePicker.SelectedDate}" ></TextBlock>
|
<TextBlock Text="{Binding #singlePicker.SelectedDate}" ></TextBlock>
|
||||||
<u:DatePicker Name="singlePicker" Width="200" Classes="ClearButton" />
|
<u:DatePicker Name="singlePicker" Width="200" Classes="ClearButton" />
|
||||||
<u:DateRangePicker Width="300" DisplayFormat="yyyyMMdd" Classes="ClearButton" />
|
<u:DateRangePicker Width="300" DisplayFormat="yyyyMMdd" Classes="ClearButton" />
|
||||||
|
|
||||||
|
<TextBlock Text="Binding"></TextBlock>
|
||||||
|
<u:DatePicker Width="200" SelectedDate="{Binding SelectedDate, Mode=TwoWay}"/>
|
||||||
|
<u:DateRangePicker Width="300" SelectedStartDate="{Binding StartDate}" SelectedEndDate="{Binding EndDate}"/>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
</UserControl>
|
</UserControl>
|
||||||
|
|||||||
@@ -1,6 +1,28 @@
|
|||||||
namespace Ursa.Demo.ViewModels;
|
using System;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
|
|
||||||
public class DatePickerDemoViewModel
|
namespace Ursa.Demo.ViewModels;
|
||||||
|
|
||||||
|
public partial class DatePickerDemoViewModel: ObservableObject
|
||||||
{
|
{
|
||||||
|
[ObservableProperty] private DateTime? _selectedDate;
|
||||||
|
[ObservableProperty] private DateTime? _startDate;
|
||||||
|
[ObservableProperty] private DateTime? _endDate;
|
||||||
|
|
||||||
|
public DatePickerDemoViewModel()
|
||||||
|
{
|
||||||
|
SelectedDate = DateTime.Today;
|
||||||
|
StartDate = DateTime.Today;
|
||||||
|
EndDate = DateTime.Today.AddDays(7);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnPropertyChanged(PropertyChangedEventArgs e)
|
||||||
|
{
|
||||||
|
base.OnPropertyChanged(e);
|
||||||
|
if (e.PropertyName == nameof(SelectedDate))
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -52,16 +52,7 @@ public class DatePicker: DatePickerBase, IClearControl
|
|||||||
|
|
||||||
private void OnSelectionChanged(AvaloniaPropertyChangedEventArgs<DateTime?> args)
|
private void OnSelectionChanged(AvaloniaPropertyChangedEventArgs<DateTime?> args)
|
||||||
{
|
{
|
||||||
if (args.NewValue.Value is null)
|
SyncSelectedDateToText(args.NewValue.Value);
|
||||||
{
|
|
||||||
_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)
|
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||||
@@ -91,6 +82,7 @@ public class DatePicker: DatePickerBase, IClearControl
|
|||||||
{
|
{
|
||||||
_calendar.DateSelected += OnDateSelected;
|
_calendar.DateSelected += OnDateSelected;
|
||||||
}
|
}
|
||||||
|
SyncSelectedDateToText(SelectedDate);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDateSelected(object? sender, CalendarDayButtonEventArgs e)
|
private void OnDateSelected(object? sender, CalendarDayButtonEventArgs e)
|
||||||
@@ -122,6 +114,20 @@ public class DatePicker: DatePickerBase, IClearControl
|
|||||||
SetSelectedDate(true);
|
SetSelectedDate(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void SyncSelectedDateToText(DateTime? date)
|
||||||
|
{
|
||||||
|
if (date is null)
|
||||||
|
{
|
||||||
|
_textBox?.SetValue(TextBox.TextProperty, null);
|
||||||
|
_calendar?.ClearSelection();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_textBox?.SetValue(TextBox.TextProperty, date.Value.ToString(DisplayFormat ?? "yyyy-MM-dd"));
|
||||||
|
_calendar?.MarkDates(startDate: date.Value, endDate: date.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void SetSelectedDate(bool fromText = false)
|
private void SetSelectedDate(bool fromText = false)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(_textBox?.Text))
|
if (string.IsNullOrEmpty(_textBox?.Text))
|
||||||
|
|||||||
@@ -165,6 +165,42 @@ public class DateRangePicker : DatePickerBase, IClearControl
|
|||||||
_endCalendar.DatePreviewed += OnDatePreviewed;
|
_endCalendar.DatePreviewed += OnDatePreviewed;
|
||||||
_endCalendar.ContextDateChanged += OnContextDateChanged;
|
_endCalendar.ContextDateChanged += OnContextDateChanged;
|
||||||
}
|
}
|
||||||
|
SyncDateToText();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SyncDateToText()
|
||||||
|
{
|
||||||
|
if (SelectedStartDate is not null)
|
||||||
|
{
|
||||||
|
_startTextBox?.SetValue(TextBox.TextProperty, SelectedStartDate.Value.ToString(DisplayFormat ?? "yyyy-MM-dd"));
|
||||||
|
}
|
||||||
|
if (SelectedEndDate is not null)
|
||||||
|
{
|
||||||
|
_endTextBox?.SetValue(TextBox.TextProperty, SelectedEndDate.Value.ToString(DisplayFormat ?? "yyyy-MM-dd"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (SelectedStartDate is null)
|
||||||
|
{
|
||||||
|
_startCalendar?.ClearSelection();
|
||||||
|
}
|
||||||
|
if(SelectedEndDate is null)
|
||||||
|
{
|
||||||
|
_endCalendar?.ClearSelection();
|
||||||
|
}
|
||||||
|
if(SelectedStartDate is not null && SelectedEndDate is not null)
|
||||||
|
{
|
||||||
|
_startCalendar?.MarkDates(SelectedStartDate, SelectedEndDate);
|
||||||
|
_endCalendar?.MarkDates(SelectedStartDate, SelectedEndDate);
|
||||||
|
}
|
||||||
|
else if(SelectedStartDate is not null)
|
||||||
|
{
|
||||||
|
_startCalendar?.MarkDates(SelectedStartDate, SelectedStartDate);
|
||||||
|
}
|
||||||
|
else if(SelectedEndDate is not null)
|
||||||
|
{
|
||||||
|
_endCalendar?.MarkDates(SelectedEndDate, SelectedEndDate);
|
||||||
|
}
|
||||||
|
PseudoClasses.Set(PseudoClassName.PC_Empty, SelectedStartDate is null && SelectedEndDate is null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnTextBoxLostFocus(object? sender, RoutedEventArgs e)
|
private void OnTextBoxLostFocus(object? sender, RoutedEventArgs e)
|
||||||
@@ -381,4 +417,6 @@ public class DateRangePicker : DatePickerBase, IClearControl
|
|||||||
|
|
||||||
SetCurrentValue(IsDropdownOpenProperty, true);
|
SetCurrentValue(IsDropdownOpenProperty, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user