Skip to main content

Microsoft Silverlight

Answered Question DatePicker shows Time?RSS Feed

(0)

mtiede@swtechnologies.com
mtiede@s...

Member

Member

148 points

275 Posts

DatePicker shows Time?

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
elmore.adam

Member

Member

482 points

67 Posts

Answered Question

Re: DatePicker shows Time?

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\}}" 

--
Kind Regards

Adam Elmore
MCTS: WPF, WCF

417.200.4261
elmore.adam@gmail.com
http://adamelmore.net/

mtiede@swtechnologies.com
mtiede@s...

Member

Member

148 points

275 Posts

Re: DatePicker shows Time?

 Excellent!  You saved me another day of futzing around trying to figure that out!

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities