Lets say i have a TextBlock thats bound to a DateTime, is there any way to replace the value 0001-01-01 00:00:00 with an empty string?
+1
A:
Use a converter to format the date
Converter code :
public class MyDateConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
DateTime dt = (DateTime)value;
if (dt == dt.MinValue)
return "";
else
return dt.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string s = (string)value;
if (string.IsNullOrEmpty(s))
return DateTime.MinValue;
else
return DateTime.Parse(s);
}
}
XAML :
...
<Window.Resources>
<local:MyDateConverter x:Key="dateConverter"/>
</Window.Resources/>
...
<TextBlock Text="{Binding TheDate, Converter={StaticResource dateConverter}}"/>
Thomas Levesque
2009-08-27 13:15:07
and how would i use this in WPF?
Petoj
2009-08-27 13:22:55
see my updated answer
Thomas Levesque
2009-08-27 13:59:50
Note that a ConvertBack implementation is unnecessary because you're binding to TextBlock.Text, which can only be one-way.
Kent Boogaart
2009-08-27 15:21:48
yes, you're right... I read (and wrote) "TextBlock", but I was actually thinking "TextBox" ;)
Thomas Levesque
2009-08-27 15:31:05
+3
A:
You have a few options:
- Bind to a
DateTime?
(that is,Nullable<DateTime>
) rather thanDateTime
. Set the value tonull
when you want nothing to appear. - Bind to a separate property on your view model which is responsible for converting
DateTime.MinValue
to an emptystring
. - Bind directly to the DateTime property and use a converter to convert
DateTime.MinValue
to an emptystring
.
Example of #1
<TextBlock Text="{Binding SomeDateTime}"/>
public class YourViewModel : ViewModel
{
private DateTime? _someDateTime;
public DateTime? SomeDateTime
{
get { return _someDateTime; }
set
{
if (_someDateTime != value)
{
_someDateTime = value;
OnPropertyChanged("SomeDateTime");
}
}
}
}
Example of #2
<TextBlock Text="{Binding SomeDateTimeString}"/>
public class YourViewModel : ViewModel
{
private DateTime _someDateTime;
public DateTime SomeDateTime
{
get { return _someDateTime; }
set
{
if (_someDateTime != value)
{
_someDateTime = value;
OnPropertyChanged("SomeDateTime");
OnPropertyChanged("SomeDateTimeString");
}
}
}
public string SomeDateTimeString
{
get { return SomeDateTime == DateTime.MinValue ? "" : SomeDateTime; }
}
}
Example of #3
<!-- in resources -->
<local:DateTimeConverter x:Key="YourConverter"/>
<TextBlock Text="{Binding SomeDateTime, Converter={StaticResource YourConverter}}"/>
public class YourClass
{
private DateTime _someDateTime;
public DateTime SomeDateTime
{
get { return _someDateTime; }
set
{
if (_someDateTime != value)
{
_someDateTime = value;
OnPropertyChanged("SomeDateTime");
}
}
}
}
public class DateTimeConverter : IValueConverter
{
public object Convert(object value ...)
{
return value == DateTime.MinValue ? "" : value;
}
...
}
HTH, Kent
Kent Boogaart
2009-08-27 13:47:26
I think you also need to call OnPropertyChanged("SomeDateTimeString") in your example #2
Julien Poulin
2009-08-27 18:46:54