Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

datetimepicker binding issues

I'm trying to use the new dateTimePicker for Windows 8.1:

<DatePicker HorizontalAlignment="Left" Margin="401,245,0,0" Grid.Row="1"
 VerticalAlignment="Top" Width="352" Date="{Binding personSingle.personDOB,Mode=TwoWay}"/>

When ever I change the date I don't get the value that I chose when I look at value for personDOB. personDOB is of type DateTimeOffset

What do I need to do get the value that I choose?

Update:

    <DatePicker x:Name="dtPick" HorizontalAlignment="Left" Margin="401,245,0,0" Grid.Row="1" 
VerticalAlignment="Top" Width="352" DataContext="{Binding personSingle}"
 Date="{Binding personSingle.personDOB.Date,Mode=TwoWay}"/>
like image 472
webdad3 Avatar asked Oct 01 '13 01:10

webdad3


People also ask

What is DateTimePicker control?

A date and time picker (DTP) control provides a simple and intuitive interface through which to exchange date and time information with a user. For example, with a DTP control you can ask the user to enter a date and then easily retrieve the selection.

What is DateTimePicker (); in JS?

The JavaScript DateTime Picker is a lightweight and mobile-friendly control that allows end users to enter or select date and time values from a pop-up calendar and drop-down time list. It provides month, year, and decade views for quick navigation to the desired date.

What is DateTimePicker type?

In Windows Forms, the DateTimePicker control is used to select and display the date/time with a specific format in your form.


1 Answers

I found the answer from this link:

http://bretstateham.com/binding-to-the-new-xaml-datepicker-and-timepicker-controls-to-the-same-datetime-value/

You need to write a converter to get this to work appropriately:

public class DateTimeToDateTimeOffsetConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        try
        {
            DateTime date = (DateTime)value;
            return new DateTimeOffset(date);
        }
        catch (Exception ex)
        {
            return DateTimeOffset.MinValue;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        try
        {
            DateTimeOffset dto = (DateTimeOffset)value;
            return dto.DateTime;
        }
        catch (Exception ex)
        {
            return DateTime.MinValue;
        }
    }
}
like image 80
webdad3 Avatar answered Sep 30 '22 14:09

webdad3