-1

Re-worked as per Peter’s suggestion.

I have an xceed DateTimePicker. This accepts a DateTime? value to display and bind to.

I have a special date – 1900-01-01 00:00:00

If I pass this date into the DateTimePicker, I would like this to be treated as if I passed in null – ie, a date is not shown.

When I retrieve the value, if the date hasn’t been set ie null, I would like to return that same special date – 1900-01-01 00:00:00.

For all other dates, whatever goes in goes out.

In code behind I could do it like this (pseudo code):

        DateTime SpecialDate = new DateTime(1900,1,1,0,0,0)
        DateTime TestDate = new DateTime(2016, 2, 8, 10, 0, 0);

        DateTimePicker dtp = new DateTimePicker();


        // Setting value.
        if (TestDate != SpecialDate)
            dtp.Value = TestDate;

        // Getting value
        if (dtp.Value == null)
            return SpecialDate;
        else
            return (DateTime)dtp.Value;

Can I achieve the same kind of thing using Xaml and binding ?

andrew
  • 1,184
  • 3
  • 19
  • 28
  • It's not a attached property but a dependency property. Second you do not need a INotifyPropertyChanged to reflect a change on UI if a property is dependency property. and my question is if you want a nullable date why is FilteredDate is not a nullable property? – Kylo Ren Feb 08 '16 at 06:26
  • The Value property of DateTimePicker. what kind of property is it? I guess this is the value that is being displayed in editor. – Kylo Ren Feb 08 '16 at 06:30
  • The data i've got is DateTime - which is not nullable. But DateTimePicker is; but that's not the problem. I'm trying to deal with a sentinal date which represents null... but isn't null. I can't change that; but don't want to display the sentinal date in the DateTimePicker. – andrew Feb 08 '16 at 06:55
  • On Inotify, that could be something else I need to look into. Presently it's not holding a value - returns another property. I'll see if having a value makes this work – andrew Feb 08 '16 at 06:56
  • So your problem is you are not able to set a blank value in the editor when actual value is sentinal date or you are not at all able to set a value? – Kylo Ren Feb 08 '16 at 06:58
  • and try this "DateTime.Now != DateTime.Now" gives true, so not sure if your check in the setter is working fine. – Kylo Ren Feb 08 '16 at 07:01
  • Right now neither are working. If I ditch the whole problem i'm trying to solve, I can get the datetimepicker to show the sentinal date no worries. – andrew Feb 08 '16 at 07:01
  • The setter is trying to trap for the sentinal; if so, don't set.. hopefully meaning the value shown is not set. If I pass in any other date, that's not showing which is why I know my code isn't right. – andrew Feb 08 '16 at 07:04
  • _"I need a kind of translator."_ -- usually, that means you need to write an `IValueConverter` (or in some cases, `IMultiValueConverter`) and use that in your binding. Unfortunately, it's not really clear what you want. Please edit your question so that it includes _less_ descriptive text, along with a good [mcve], and a _concise_ description of what the code does and how that's different from what you want. – Peter Duniho Feb 08 '16 at 07:22

1 Answers1

0

Use a simple converter to convert and convertback values:

public class DateTimeToSentinalDateConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (System.Convert.ToDateTime(value).Equals(new DateTime(1900, 1, 1, 0, 0, 0)))
        {
            return null;
        }
        else
        {
            return value;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
        {
            return new DateTime(1900, 1, 1, 0, 0, 0);
        }
        else
        {
            return value;
        }
    }
}

And in the XAMl the binding will be:

Value="{Binding Path=Date , Converter={StaticResource DateTimeToSentinalDateConverter}}"
Kylo Ren
  • 8,551
  • 6
  • 41
  • 66