Click here to Skip to main content
15,894,646 members
Articles / Desktop Programming / WPF

Validizor - A Validation Control for WPF

Rate me:
Please Sign up or sign in to vote.
4.66/5 (16 votes)
17 Oct 2007CPOL7 min read 94.6K   4.3K   58  
A WPF validation control for validating your data objects.
//---------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation.  All rights reserved.
//
//---------------------------------------------------------------------------

using System;
using System.Globalization;
using System.Windows.Data;


namespace Microsoft.Samples.KMoore.WPFSamples.DateControls
{
    /// <summary>
    /// Convert between DateTime and string, it's the default Converter for DatePicker.DateValueConverter
    /// </summary>
    public sealed class DateTimeValueConverter : IValueConverter
    {
        /// <summary>
        /// Convert DateTime to a formatted string(ShortDatePattern)
        /// </summary>
        /// <param name="value">DateTime</param>
        /// <param name="targetType">string</param>
        /// <param name="parameter">null</param>
        /// <param name="culture"></param>
        /// <returns>formatted or empty string</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (culture == null)
            {
                throw new ArgumentNullException("culture");
            }
            DateTimeFormatInfo dateTimeFormat = culture.DateTimeFormat;
            if (dateTimeFormat == null)
            {
                throw new ArgumentNullException("culture.DateTimeFormat");
            }

            if (value != null && value is DateTime)
            {
                DateTime date = (DateTime)value;
                return date.ToString(dateTimeFormat.ShortDatePattern, dateTimeFormat);
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// Convert input string to DateTime
        /// </summary>
        /// <param name="value">string</param>
        /// <param name="targetType">DateTime</param>
        /// <param name="parameter">null</param>
        /// <param name="culture"></param>
        /// <returns>DateTime or null</returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (culture == null)
            {
                throw new ArgumentNullException("culture");
            }

            if (value != null && value is string)
            {
                return DateTime.Parse((string)value, culture.DateTimeFormat);
            }
            else
            {
                return null;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
software engineer

Comments and Discussions