wpf日期控件

  /// 
    /// Value converter to convert a datetime object to the specified string format. 
    ///   If the format is not specified, it will be converted to short date string "12/31/2011"
    /// 
    [ValueConversion(typeof(DateTime), typeof(string))]
    public class CalendarConverter : IValueConverter
    {
        #region IValueConverter Members

        /// 
        /// Implement the ConvertBack method of IValueConverter. Converts DateTime object to specified format
        /// 
        /// The DateTime value we're converting
        /// Not used
        /// String format to convert to (optional)
        /// Not used
        /// Collapsed if value is true, else Visible
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            DateTime date = (DateTime)value;
            string param = parameter as string;

            return string.IsNullOrEmpty(param) ? date.ToShortDateString() : date.ToString(param);
        }

        /// 
        /// Implement the Convert method of IValueConverter. Converts a string representation of a date to DateTime
        /// 
        /// The visibility value to convert to a boolean.
        /// Not used
        /// Not used
        /// Not used
        /// false if Visible, else true
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string dateString = (string)value;
            DateTime resultDateTime;

            if (DateTime.TryParse(dateString, out resultDateTime))
                return resultDateTime;

            return DateTime.Now;
        }

        #endregion
    }

调用:



  

你可能感兴趣的:(wpf日期控件)