Skip to content

Commit ca48d57

Browse files
committed
fix converter issue (in design time),
1 parent e73928e commit ca48d57

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

Diff for: UnityLauncherPro/Converters/ReleaseDateConverter.cs

+30-10
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,49 @@
11
using System;
22
using System.Globalization;
3+
using System.Windows;
34
using System.Windows.Data;
45

56
namespace UnityLauncherPro.Converters
67
{
7-
[ValueConversion(typeof(DateTime), typeof(String))]
8+
[ValueConversion(typeof(DateTime), typeof(string))]
89
public class ReleaseDateConverter : IValueConverter
910
{
1011
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
1112
{
12-
if (value == null) return null;
13-
DateTime date = (DateTime)value;
13+
if (value == null || !(value is DateTime date))
14+
{
15+
return DependencyProperty.UnsetValue;
16+
}
1417

15-
// get first part of string until space character (updates only contain mm/dd/yyyy)
16-
string dateStrTrimmed = MainWindow.currentDateFormat;
17-
if (dateStrTrimmed.IndexOf(' ') > -1) dateStrTrimmed = dateStrTrimmed.Split(' ')[0];
18+
// Use a default date format if currentDateFormat is null or empty
19+
string dateStrTrimmed = MainWindow.currentDateFormat ?? "MM/dd/yyyy";
1820

19-
return MainWindow.useHumanFriendlyDateFormat ? Tools.GetElapsedTime(date) : date.ToString(dateStrTrimmed);
21+
// If the format includes time, use only the date portion
22+
if (dateStrTrimmed.Contains(" "))
23+
{
24+
dateStrTrimmed = dateStrTrimmed.Split(' ')[0];
25+
}
26+
27+
// Return a human-friendly format if enabled; otherwise, format based on dateStrTrimmed
28+
return MainWindow.useHumanFriendlyDateFormat
29+
? Tools.GetElapsedTime(date)
30+
: date.ToString(dateStrTrimmed, culture);
2031
}
2132

2233
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
2334
{
24-
// not used ?
25-
return DateTime.ParseExact((string)value, MainWindow.currentDateFormat, culture);
26-
}
35+
if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
36+
{
37+
return DependencyProperty.UnsetValue;
38+
}
2739

40+
// Attempt to parse back to DateTime using the specified format
41+
if (DateTime.TryParseExact((string)value, MainWindow.currentDateFormat ?? "MM/dd/yyyy", culture, DateTimeStyles.None, out DateTime parsedDate))
42+
{
43+
return parsedDate;
44+
}
45+
46+
return DependencyProperty.UnsetValue;
47+
}
2848
}
2949
}

Diff for: UnityLauncherPro/MainWindow.xaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,8 @@
487487
<MenuItem x:Name="menuItemUpdatesReleaseNotes" Header="Open Release Notes" Click="MenuItemUpdatesReleaseNotes_Click" />
488488
</ContextMenu>
489489
</DataGrid.ContextMenu>
490-
<local:UnityVersion ReleaseDate="2020-10-10" Version="5000.1.2.3"/>
491-
492490
<!-- sample data for testing -->
491+
<local:UnityVersion ReleaseDate="2020-10-10" Version="5000.1.2.3"/>
493492
</DataGrid>
494493

495494
<!-- bottom buttoms row -->

0 commit comments

Comments
 (0)