I have this converter that i made to get the current time once the date is selected from the DataPicker. In string Date i am getting the value that was selected from the DatePicker, but i cant seem to only get the date. The format that is coming into the Value property is 9/24/2013 12:00:00 I would like it to be 9/24/2013
the error i am getting is "Error 122 No overload for method 'ToString' takes 1 argument"
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is DateTime)
{
string date = value.ToString("d/M/yyyy");
return (date);
}
return string.Empty;
}
You need to cast it to DateTime first:
After check on type of value you need cast it to appropriate type, to be able to perform "ToString" call with format parameter. Try:
If you are using a Converter on a WPF DatePicker control, you should note that the WPF DatePicker will itself reformat the date despite the converter that you use. You will have to style the Datepicker to include a StringFormat.
There is a related question here: how to show just Month Year string format in a DatePicker which shows an attached property to modify the behaviour of DatePicker to display a custom format. This is needed because of a deficiency in the WPF Datepicker control itself.
Also note there are some caveats, notably the DatePicker will flicker between its default stringformat and the one you apply! I answered in the above question a workaround for to apply a custom Format to WPF Datepicker without the flicker.
Hope you find the solution you are looking for.
You should cast value to
DateTime
type, because there is not aToString(String f)
method for the type ofObject
.