.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > Binding to date on new object shows DateTime.MinValue. Can this be hidden?
 

Binding to date on new object shows DateTime.MinValue. Can this be hidden?

I've got a DatePicker field that's bound to an underlying object. When I create a new object and bind it to the screen, it displays DateTime.MinValue (1/1/0001), which is expected. But, I don't want the user to see that, I want them to see an empty text-box.

How can I accomplish this?

Thanks,
Chris

CCusson15
You can do an IValueConverter to handle this and put this converter in the binding. For example:


public class DateTimeNullConverter : IValueConverter

{

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

{

if (value is DateTime)

{

DateTime typedValue = (DateTime)value;

if (typedValue == DateTime.MinValue)

{

return string.Empty;

}

return typedValue.ToString();

}

throw new InvalidOperationException();

}

Mariano O. Rodriguez
Hi CCussson15,
I have answered a similar question here http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b3c600cc-c848-4733-b193-9c2c39db7ae5

The answer is about the same. although the converter would be different:

The easiest way to me is to use a converter, taking in a DateTime, returning a string.
In converter:
1. Cast value to DateTime.
2. Compare to DateTime.MinValue
3. If true return string.Empty, if not DateTime.toString()

Hope this helps!

-noorbakhsh
noorbakhsh
You can do an IValueConverter to handle this and put this converter in the binding. For example:


public class DateTimeNullConverter : IValueConverter

{

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

{

if (value is DateTime)

{

DateTime typedValue = (DateTime)value;

if (typedValue == DateTime.MinValue)

{

return string.Empty;

}

return typedValue.ToString();

}

throw new InvalidOperationException();

}

Mariano O. Rodriguez
Hi CCussson15,
I have answered a similar question here http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b3c600cc-c848-4733-b193-9c2c39db7ae5

The answer is about the same. although the converter would be different:

The easiest way to me is to use a converter, taking in a DateTime, returning a string.
In converter:
1. Cast value to DateTime.
2. Compare to DateTime.MinValue
3. If true return string.Empty, if not DateTime.toString()

Hope this helps!

-noorbakhsh
noorbakhsh
Thanks noorbakhsh and Mariano, this works great!!

One question, what should i put in for the ConvertBack method? I'm thinking this isn't something I need to do (though I need to code it bc of the interface).

Thanks,
Chris
CCusson15
If you are using TwoWay mode you will need to implement this method too. Basically you have to convert from string to DateTime.
Mariano O. Rodriguez
You could make the datetime object nullable.
mariocatch

You can use google to search for other answers

Custom Search

More Threads

• Problem with INotifyPropertyChanged
• Binding a slider and 6 radio buttons
• WPF Ribbon - ContextualTabGroups Color
• Mouse clicking problem
• How to WPF unit test with visual studio 2008?
• ListboxItem - how do i do in C# ?
• WPF with C# only?
• How to make an avi of animation
• PageFunction and resources
• Should I really be this confused on something so easy?