Skip to main content
Home Forums General Silverlight New Features in Silverlight 3 DatePicker shows Time?
2 replies. Latest Post by mtiede@swtechnologies.com on July 9, 2009.
(0)
mtiede@s...
Member
148 points
275 Posts
07-09-2009 11:23 AM |
I have bound the DatePicker to nullable datetime datamember of a wcf datacontract. I have a Fetch button that, when pressed, sets the datacontext and I see a date from my Oracle database.
If I then save the data using wcf to Oracle and press the Fetch button again, the DatePicker now shows a date AND time. If I just restart the app and Fetch the same record again, it doesn't display the time.
How can I permanently get it to just show the date?
elmore.adam
482 points
67 Posts
07-09-2009 1:00 PM |
Unfortunately, there won't be a way of doing this directly in the DatePicker control. While it does have a SelectedDateFormat property, this only accepts two values: Long and Short (default). Neither of these choices will remove the time from your display value. This means that you will need to format the value using either a wrapped property in your ViewModel, or a converter. If you go the converter route, I would suggest using a generic format converter, as opposed to a specific date format converter.
public class FormatConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
if (parameter != null)
string formatterString = parameter.ToString();
if (!string.IsNullOrEmpty(formatterString))
return string.Format(culture, formatterString, value);
}
return value.ToString();
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
return value;
#endregion
<Grid>
<Grid.Resources>
<converters:FormatConverter
x:Key="Converter_Format" />
</Grid.Resources>
<DatePicker
Text="{Binding Path=SelectedDate, RelativeSource={RelativeSource Self}, Converter={StaticResource Converter_Format}, ConverterParameter=\{0:d\}}"
07-09-2009 1:30 PM |
Excellent! You saved me another day of futzing around trying to figure that out!