Skip to content

Commit 8507ee0

Browse files
committed
fix clock binding warning fix #150
1 parent 0f8de61 commit 8507ee0

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

MaterialDesignThemes.Wpf/Clock.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public Clock()
6262
public static readonly DependencyProperty TimeProperty = DependencyProperty.Register(
6363
"Time", typeof (DateTime), typeof (Clock), new FrameworkPropertyMetadata(default(DateTime), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, TimePropertyChangedCallback));
6464

65-
private static void TimePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
65+
private static void TimePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
6666
{
6767
var clock = (Clock) dependencyObject;
6868
SetFlags(clock);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Windows.Data;
8+
9+
namespace MaterialDesignThemes.Wpf.Converters
10+
{
11+
public class NullableDateTimeToCurrentDateConverter : IValueConverter
12+
{
13+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
14+
{
15+
if (value is DateTime)
16+
return value;
17+
return DateTime.Now.Date;
18+
}
19+
20+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
21+
{
22+
return value;
23+
}
24+
}
25+
}

MaterialDesignThemes.Wpf/MaterialDesignThemes.Wpf.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@
228228
<Compile Include="Converters\MathConverter.cs" />
229229
<Compile Include="Converters\MathMultipleConverter.cs" />
230230
<Compile Include="Converters\NotConverter.cs" />
231+
<Compile Include="Converters\NullableDateTimeToCurrentDateConverter.cs" />
231232
<Compile Include="Converters\NullToVisibilityConverter.cs" />
232233
<Compile Include="Converters\NotZeroToVisibilityConverter.cs" />
233234
<Compile Include="Converters\ShadowConverter.cs" />

MaterialDesignThemes.Wpf/TimePicker.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Windows.Data;
77
using System.Windows.Input;
88
using System.Windows.Threading;
9+
using MaterialDesignThemes.Wpf.Converters;
910

1011
namespace MaterialDesignThemes.Wpf
1112
{
@@ -352,7 +353,7 @@ private void InitializeClock()
352353
_clock.AddHandler(Clock.ClockChoiceMadeEvent, new ClockChoiceMadeEventHandler(ClockChoiceMadeHandler));
353354
_clock.SetBinding(ForegroundProperty, GetBinding(ForegroundProperty));
354355
_clock.SetBinding(StyleProperty, GetBinding(ClockStyleProperty));
355-
_clock.SetBinding(Clock.TimeProperty, GetBinding(SelectedTimeProperty));
356+
_clock.SetBinding(Clock.TimeProperty, GetBinding(SelectedTimeProperty, new NullableDateTimeToCurrentDateConverter()));
356357
_clock.SetBinding(Clock.Is24HoursProperty, GetBinding(Is24HoursProperty));
357358
_clockHostContentControl.SetBinding(StyleProperty, GetBinding(ClockHostContentControlStyleProperty));
358359
}
@@ -384,10 +385,14 @@ private void TogglePopup()
384385
}
385386
}
386387

387-
private BindingBase GetBinding(DependencyProperty property)
388+
private BindingBase GetBinding(DependencyProperty property, IValueConverter converter = null)
388389
{
389-
var binding = new Binding(property.Name) {Source = this};
390-
return binding;
390+
var binding = new Binding(property.Name)
391+
{
392+
Source = this,
393+
Converter = converter
394+
};
395+
return binding;
391396
}
392397
}
393398
}

0 commit comments

Comments
 (0)