I know this is an old post, but I am doing an update anyway. I have this type of binding which have solved with a converter. I think it has been working earlier, but today I noticed that I got DateTime.Now as value in my RadMaskedDateTimeInput. The binding is working except for the initial value which is set to DateTime.Now in UI.This value is not propagated to the binding, but when I edit the UI I get the right value.
I set the Duration to 20 min by default but still I only get DateTime.Now. I have tried to bind to a DateTime? property without the converter but I still only get DateTime.Now in my UI.
Here is the
control:
<
telerik:RadMaskedDateTimeInput
x:Name
=
"DurationInput"
EmptyContent
=
"{dt:Text HHMM}"
InputBehavior
=
"Replace"
Mask
=
"HH:mm"
SelectionOnFocus
=
"SelectAll"
TextMode
=
"PlainText"
UpdateValueEvent
=
"LostFocus"
Value
=
"{Binding Duration, Mode=TwoWay, Converter={StaticResource TimeSpanDateTimeConverter}}"
/>
And this is my converter:
using
System;
using
System.Globalization;
using
System.Windows.Data;
public
class
TimeSpanDateTimeConverter : IValueConverter
{
public
object
Convert(
object
value, Type targetType,
object
parameter, CultureInfo culture)
{
// TimeSpan in
if
(!(value
is
TimeSpan))
{
return
default
(DateTime?);
}
var timeSpan = (TimeSpan) value;
// DateTime out
if
(targetType !=
typeof
(DateTime?))
{
return
null
;
}
var nowDate = DateTime.Now.Date;
return
nowDate + timeSpan;
}
public
object
ConvertBack(
object
value, Type targetType,
object
parameter, CultureInfo culture)
{
// DateTime in
var dateTime = (DateTime?) value;
// TimeSpan out
return
targetType !=
typeof
(TimeSpan?)
?
null
: dateTime?.TimeOfDay;
}
}
And this is my view-model property:
public
TimeSpan? Duration
{
get
=> _duration;
set
{
SetProperty(
ref
_duration, value);
}
}
I am using Telerik 2020.1.115.
Best regards
Ola