DateTimeConverter

 

  xmlns:Converters="clr-namespace:SBMI.rEHR.Utils.Converter"

   <Local:View.Resources>

        <Converters:DateTimeConverter x:Key="DateTimeConverter"/>

    </Local:View.Resources>

Converter={StaticResource DateTimeConverter},ConverterParameter='DateTimeString'

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Globalization;

using System.Windows.Data;

 

/********************************************

 * quietwalk

 * 06/26/2012

 *

 * ******************************************/

 

namespace SBMI.rEHR.Utils.Converter

{

    /// <summary>

    /// Convert DateTime to: DateTimeString MM/dd/yyyy; DateString; TimeString

    /// </summary>

    public class DateTimeConverter : IValueConverter

    {

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

        {

            if (value == null) return null;

 

            DateTime dt = System.Convert.ToDateTime(value);

            if (dt == null) return null;

 

            if (parameter != null)

            {

                string strResult = string.Empty;

                string param = parameter as string;

 

                if (string.IsNullOrEmpty(param) == false)

                {

                    if (param.ToLower() == "datetimestring")

                    {

                        strResult = dt.ToString("MM/dd/yyyy");

                    }

                    else if (param.ToLower() == "datestring")

                    {

                        //strResult = string.Format("{0:d}", dt);

                        strResult = dt.ToString("MM/dd/yyyy");

                    }

                    else if (param.ToLower() == "timestring")

                    {

                        strResult = string.Format("{0:T}", dt);

                    }

                    return strResult;

                }

            }//if

 

            return null;

        }

 

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

        {

 

            throw new NotImplementedException();

        }

    }

}

 

原文地址:https://www.cnblogs.com/quietwalk/p/2563468.html