0

I'm trying to get a DateTimePicker object to populate with the default text (Select a date) when it doesn't get any date back from the database. If there is a date in the database, the field will populate with that date.

I wroting code that has a two way bind on SelectedDate option to a DateTime property on in the code-behind. It works properly and populates the field with 01/01/0001 since that is the null of DateTime objects. I've tried to changing it to a OneWayToSource and just bind the date if it is greater than 01/01/0001 but it puts a redbox around the object if it doesn't get a date.

Any suggestion?

Thanks for the help everyone! Here is the solution that I found.

[ValueConversion(typeof(DateTime), typeof(DateTime))]
class DateTimeNullConverter: IValueConverter
    {
         public object Convert (object value, Type targetType, object parameter, Culture culture)
         {
                if (value != null)
                {
                   DateTime dateTime = (DateTime)value;
                   if (dateTime.Year.ToString == "1")
                       return null;
                   else
                      return dateTime;
                }
                else
                {
                      return null;
                }
            }

         public object ConvertBack (object value, Type targetType, object parameter, Culture culture)
         {
              DateTime convertDateTime;
              if (value == null)
              {
                  convertDateTime = new DateTime();
              }
              else
              {
                  convertDateTime = (DateTime) value;
              }

              return convertDateTime;
         }
    }
  • Not a dupe of, but strongly related to: http://stackoverflow.com/questions/284364/how-to-alter-a-net-datetimepicker-control-to-allow-enter-null-values – GWLlosa Apr 07 '15 at 16:22

3 Answers3

0

Create a DateBlankConverter converter that binds to the same control:

 <DatePicker x:Name="DatePickerInstance" 
 Visibility="{Binding ElementName=DatePickerInstance, 
 Converter={StaticResource DateBlankConverter}, ConverterParameter={Binding Date}}"/>

And inside the converter check if the date is null to hide or show the DatePicker, or change the property you need.

Juan Pablo Garcia Coello
  • 3,192
  • 1
  • 23
  • 33
  • I thought about exactly what you were thinking also but the "SelectedDate" property only binds to DateTime objects. DateTime object doesn't have a null option. A newly created DateTime object is set to 01/01/0001 (equivalent to null). – Nick Van Meter Apr 07 '15 at 17:37
  • Actually, it looks like I lied. Thanks for that suggestion again. I swore the field only took DateTime objects. I looked at the "SelectedDate" property again and it also takes a NULL value. I wrote a converter for it. – Nick Van Meter Apr 07 '15 at 18:51
  • Thank you, Indeed yesterday I took a look to the Windows 10 DatePicker is totally new I made an article here http://bit.ly/1GnNpxI – Juan Pablo Garcia Coello Apr 08 '15 at 09:06
0

You could try with some code in the setter and getter of your property

private DateTime? _date;
public DateTime? Date
{
    get 
    {
        if (null == _date)
        {
            //Set some initial value
            //or just return some default value without setting the property
        }
        return _date; 
    }
    set
    {
        if (value != _date)
        {
            _date = value;
            this.OnPropertyChanged("Date");
        }
    }
}
Daniel Filipov
  • 325
  • 4
  • 17
  • Good idea! The only problem is that if you bind that with SelectedDate value. Visual studio informs you that "Cannot convert null to "System.DateTime" because it is a non-nullable value type." So it wants a value of something. I don't like it but I might just take out the binding and manually set the value in code behind. (DatePickerObjName.SelectedDate = _ date) – Nick Van Meter Apr 07 '15 at 16:43
0

Thanks for the help everyone! Here is the solution that I found.

[ValueConversion(typeof(DateTime), typeof(DateTime))]
class DateTimeNullConverter: IValueConverter
    {
         public object Convert (object value, Type targetType, object parameter, Culture culture)
         {
                if (value != null)
                {
                   DateTime dateTime = (DateTime)value;
                   if (dateTime.Year.ToString == "1")
                       return null;
                   else
                      return dateTime;
                }
                else
                {
                      return null;
                }
            }

         public object ConvertBack (object value, Type targetType, object parameter, Culture culture)
         {
              DateTime convertDateTime;
              if (value == null)
              {
                  convertDateTime = new DateTime();
              }
              else
              {
                  convertDateTime = (DateTime) value;
              }

              return convertDateTime;
         }
    }