Skip to content

Commit

Permalink
improve PersianPicker Controls
Browse files Browse the repository at this point in the history
  • Loading branch information
ghost1372 committed Nov 29, 2019
1 parent 48a39f9 commit 03a028c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
<TabItem Header="Calendar">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<StackPanel Orientation="Horizontal">
<hc:PersianDateTimePicker Height="40"/>
<StackPanel>
<hc:PersianDatePicker Name="pDP" Margin="10" Width="200" />
<hc:PersianDateTimePicker Height="40"/>
</StackPanel>
<hc:PersianCalendar Margin="10"/>
<hc:PersianCalendarWithClock Margin="10" ClockConfirm="باشه"/>
<hc:PersianDatePicker Name="pDP" Margin="10" Width="200" />
</StackPanel>
</ScrollViewer>
</TabItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ public PersianDatePicker()

// Binding to FirstDayOfWeek and DisplayDate wont work
FirstDayOfWeek = DateTimeHelper.GetCurrentDateFormat().FirstDayOfWeek;
SelectedDate = DateTime.Now;
if(SelectedDate == null)
SelectedDate = DateTime.Now;

}

#region Public properties
Expand Down Expand Up @@ -480,14 +482,8 @@ private static void OnSelectedDateChanged(DependencyObject d, DependencyProperty
if (dp.SelectedDate.HasValue)
{
DateTime day = dp.SelectedDate.Value;
dp.SetTextInternal(dp.DateTimeToString(day));

// When DatePickerDisplayDateFlag is TRUE, the SelectedDate change is coming from the Calendar UI itself,
// so, we shouldn't change the DisplayDate since it will automatically be changed by the Calendar
if ((day.Month != dp.DisplayDate.Month || day.Year != dp.DisplayDate.Year) && !dp._calendar.DatePickerDisplayDateFlag)
{
dp.DisplayDate = day;
}
dp.SetTextInternal(dp.DateTimeToString(day));

dp._calendar.DatePickerDisplayDateFlag = false;
}
Expand Down Expand Up @@ -696,17 +692,6 @@ private void SetTextInternal(string value)

#endregion Public Properties

#region Protected properties

#endregion Protected Properties

#region Internal Properties

#endregion Internal Properties

#region Private Properties
#endregion Private Properties

#region Public Methods

/// <summary>
Expand Down Expand Up @@ -1044,12 +1029,36 @@ private string DateTimeToString(DateTime d)
{
case DatePickerFormat.Short:
{
return string.Format("{0:00}/{1:00}/{2:00}", pc.GetYear(d) % 100, pc.GetMonth(d), pc.GetDayOfMonth(d));

//Fix for SelectedDate in xaml that double converted so we check if year is 3 number like 777 we convert date to gregorian
int year = pc.GetYear(d) % 100;
int month = pc.GetMonth(d);
int day = pc.GetDayOfMonth(d);

if (year.ToString().Length < 4)
{
DateTime dt = new DateTime(year, month, day, pc);
year = dt.Year;
month = dt.Month;
day = dt.Day;
}
return string.Format("{0:00}/{1:00}/{2:00}", year, month, day);
}
case DatePickerFormat.Long:
{
return string.Format("{0:0000}/{1:00}/{2:00}", pc.GetYear(d), pc.GetMonth(d), pc.GetDayOfMonth(d));
int year = pc.GetYear(d);
int month = pc.GetMonth(d);
int day = pc.GetDayOfMonth(d);

if (year.ToString().Length < 4)
{
DateTime dt = new DateTime(year, month, day, pc);
year = dt.Year;
month = dt.Month;
day = dt.Day;
}

return string.Format("{0:0000}/{1:00}/{2:00}", year, month, day);

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,21 @@ private void SetSelectedDateTime()
}
}

private string DateTimeToString(DateTime d) => d.ToString(DateTimeFormat);
private string DateTimeToString(DateTime d)
{
var data = d.ToString(DateTimeFormat);

//Fix for SelectedDateTime in xaml that double converted so we check if year start with 0 we fix date
//Note: this fix work until year = 1599
if (data.StartsWith("0"))
{
var year = data.Substring(0, 4);
var month = data.Substring(5, 2);
var day = data.Substring(8, 2);
data = data.Replace(year, d.Year.ToString()).Replace(month,d.Month.ToString()).Replace(day, d.Day.ToString());
}
return data;
}

private static void OnGotFocus(object sender, RoutedEventArgs e)
{
Expand Down

0 comments on commit 03a028c

Please sign in to comment.