简体   繁体   English

如何在DateTime为空(01/01/0001)时将加载时的DateTimePicker对象保留为空白(选择日期),但在有值时填充该对象?

[英]How to leave DateTimePicker object blank (Select a date) on load when DateTime is null (01/01/0001) but populate when there is a value?

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. 我试图获取一个DateTimePicker对象,当它没有从数据库中获取任何日期时,使用默认文本(选择日期)进行填充。 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. 我把在SelectedDate选项上有两种方式绑定到隐藏代码中的DateTime属性的代码打包了。 It works properly and populates the field with 01/01/0001 since that is the null of DateTime objects. 它可以正常工作,并使用01/01/0001填充字段,因为那是DateTime对象的空值。 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. 我试图将其更改为OneWayToSource,如果日期大于01/01/0001,则仅绑定日期,但是如果没有日期,它将在对象周围放置一个红色框。

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;
         }
    }

Create a DateBlankConverter converter that binds to the same control: 创建一个绑定到相同控件的DateBlankConverter转换器:

 <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. 并在转换器内部检查日期是否为空以隐藏或显示DatePicker,或更改所需的属性。

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");
        }
    }
}

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;
         }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM