Skip to content

Commit

Permalink
Merge pull request #7194 from timunie/fix/Issue_7162_CalendarPickerBi…
Browse files Browse the repository at this point in the history
…ndings

Update Calendar in CalendarDatePicker Popup properly
  • Loading branch information
maxkatz6 authored and danwalmsley committed Dec 21, 2021
1 parent 4a3ee85 commit 23e5aaa
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 70 deletions.
68 changes: 1 addition & 67 deletions src/Avalonia.Controls/Calendar/CalendarDatePicker.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// (c) Copyright Microsoft Corporation.
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
Expand Down Expand Up @@ -425,9 +425,6 @@ static CalendarDatePicker()
{
FocusableProperty.OverrideDefaultValue<CalendarDatePicker>(true);

DisplayDateProperty.Changed.AddClassHandler<CalendarDatePicker>((x,e) => x.OnDisplayDateChanged(e));
DisplayDateStartProperty.Changed.AddClassHandler<CalendarDatePicker>((x,e) => x.OnDisplayDateStartChanged(e));
DisplayDateEndProperty.Changed.AddClassHandler<CalendarDatePicker>((x,e) => x.OnDisplayDateEndChanged(e));
IsDropDownOpenProperty.Changed.AddClassHandler<CalendarDatePicker>((x,e) => x.OnIsDropDownOpenChanged(e));
SelectedDateProperty.Changed.AddClassHandler<CalendarDatePicker>((x,e) => x.OnSelectedDateChanged(e));
SelectedDateFormatProperty.Changed.AddClassHandler<CalendarDatePicker>((x,e) => x.OnSelectedDateFormatChanged(e));
Expand Down Expand Up @@ -459,18 +456,12 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
if (_calendar != null)
{
_calendar.SelectionMode = CalendarSelectionMode.SingleDate;
_calendar.SelectedDate = SelectedDate;
SetCalendarDisplayDate(DisplayDate);
SetCalendarDisplayDateStart(DisplayDateStart);
SetCalendarDisplayDateEnd(DisplayDateEnd);

_calendar.DayButtonMouseUp += Calendar_DayButtonMouseUp;
_calendar.DisplayDateChanged += Calendar_DisplayDateChanged;
_calendar.SelectedDatesChanged += Calendar_SelectedDatesChanged;
_calendar.PointerReleased += Calendar_PointerReleased;
_calendar.KeyDown += Calendar_KeyDown;
//_calendar.SizeChanged += new SizeChangedEventHandler(Calendar_SizeChanged);
//_calendar.IsTabStop = true;

var currentBlackoutDays = BlackoutDates;
BlackoutDates = _calendar.BlackoutDates;
Expand Down Expand Up @@ -587,58 +578,6 @@ protected override void OnLostFocus(RoutedEventArgs e)
SetSelectedDate();
}

private void SetCalendarDisplayDate(DateTime value)
{
if (DateTimeHelper.CompareYearMonth(_calendar.DisplayDate, value) != 0)
{
_calendar.DisplayDate = DisplayDate;
if (DateTime.Compare(_calendar.DisplayDate, DisplayDate) != 0)
{
DisplayDate = _calendar.DisplayDate;
}
}
}
private void OnDisplayDateChanged(AvaloniaPropertyChangedEventArgs e)
{
if (_calendar != null)
{
var value = (DateTime)e.NewValue;
SetCalendarDisplayDate(value);
}
}
private void SetCalendarDisplayDateStart(DateTime? value)
{
_calendar.DisplayDateStart = value;
if (_calendar.DisplayDateStart.HasValue && DisplayDateStart.HasValue && DateTime.Compare(_calendar.DisplayDateStart.Value, DisplayDateStart.Value) != 0)
{
DisplayDateStart = _calendar.DisplayDateStart;
}
}
private void OnDisplayDateStartChanged(AvaloniaPropertyChangedEventArgs e)
{
if (_calendar != null)
{
var value = (DateTime?)e.NewValue;
SetCalendarDisplayDateStart(value);
}
}
private void SetCalendarDisplayDateEnd(DateTime? value)
{
_calendar.DisplayDateEnd = value;
if (_calendar.DisplayDateEnd.HasValue && DisplayDateEnd.HasValue && DateTime.Compare(_calendar.DisplayDateEnd.Value, DisplayDateEnd.Value) != 0)
{
DisplayDateEnd = _calendar.DisplayDateEnd;
}

}
private void OnDisplayDateEndChanged(AvaloniaPropertyChangedEventArgs e)
{
if (_calendar != null)
{
var value = (DateTime?)e.NewValue;
SetCalendarDisplayDateEnd(value);
}
}
private void OnIsDropDownOpenChanged(AvaloniaPropertyChangedEventArgs e)
{
var oldValue = (bool)e.OldValue;
Expand Down Expand Up @@ -670,11 +609,6 @@ private void OnSelectedDateChanged(AvaloniaPropertyChangedEventArgs e)
var addedDate = (DateTime?)e.NewValue;
var removedDate = (DateTime?)e.OldValue;

if (_calendar != null && addedDate != _calendar.SelectedDate)
{
_calendar.SelectedDate = addedDate;
}

if (SelectedDate != null)
{
DateTime day = SelectedDate.Value;
Expand Down
6 changes: 5 additions & 1 deletion src/Avalonia.Themes.Default/CalendarDatePicker.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@
StaysOpen="False">
<Calendar Name="PART_Calendar"
FirstDayOfWeek="{TemplateBinding FirstDayOfWeek}"
IsTodayHighlighted="{TemplateBinding IsTodayHighlighted}"/>
IsTodayHighlighted="{TemplateBinding IsTodayHighlighted}"
SelectedDate="{TemplateBinding SelectedDate, Mode=TwoWay}"
DisplayDate="{TemplateBinding DisplayDate}"
DisplayDateStart="{TemplateBinding DisplayDateStart}"
DisplayDateEnd="{TemplateBinding DisplayDateEnd}" />
</Popup>
</Grid>
</ControlTemplate>
Expand Down
6 changes: 5 additions & 1 deletion src/Avalonia.Themes.Fluent/Controls/CalendarDatePicker.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@
IsLightDismissEnabled="True">
<Calendar Name="PART_Calendar"
FirstDayOfWeek="{TemplateBinding FirstDayOfWeek}"
IsTodayHighlighted="{TemplateBinding IsTodayHighlighted}"/>
IsTodayHighlighted="{TemplateBinding IsTodayHighlighted}"
SelectedDate="{TemplateBinding SelectedDate, Mode=TwoWay}"
DisplayDate="{TemplateBinding DisplayDate}"
DisplayDateStart="{TemplateBinding DisplayDateStart}"
DisplayDateEnd="{TemplateBinding DisplayDateEnd}" />
</Popup>
</Grid>
</ControlTemplate>
Expand Down
6 changes: 5 additions & 1 deletion tests/Avalonia.Controls.UnitTests/CalendarDatePickerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ private IControlTemplate CreateTemplate()
var calendar =
new Calendar
{
Name = "PART_Calendar"
Name = "PART_Calendar",
[!Calendar.SelectedDateProperty] = control[!CalendarDatePicker.SelectedDateProperty],
[!Calendar.DisplayDateProperty] = control[!CalendarDatePicker.DisplayDateProperty],
[!Calendar.DisplayDateStartProperty] = control[!CalendarDatePicker.DisplayDateStartProperty],
[!Calendar.DisplayDateEndProperty] = control[!CalendarDatePicker.DisplayDateEndProperty]
}.RegisterInNameScope(scope);
var popup =
new Popup
Expand Down

0 comments on commit 23e5aaa

Please sign in to comment.