Click here to Skip to main content
15,895,142 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.ComponentModel;


namespace Microsoft.Samples.KMoore.WPFSamples.DateControls
{
    /// <summary>
    /// CalendarDate is a wrapper over DateTime
    /// </summary>
    /// <remarks>
    /// The main purpose of CalendarDate is to increase the MonthCalendar's scroll perf.
    /// It is used to only update Date object and fire a PropertyChanged event, 
    /// so when user scroll month, the selectedDates UI items won't be regenerated
    /// </remarks>
    public sealed class CalendarDate : INotifyPropertyChanged
    {
        #region Constructors

        /// <summary>
        /// Constructor
        /// </summary>
        public CalendarDate(DateTime date)
        {
            _date = date;
        }

        #endregion

        #region Public Properties/Events/Methods

        /// <summary>
        /// This event is raised when CalendarDate.Date has changed.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// DateTime
        /// </summary>
        public DateTime Date
        {
            get { return _date; }
            internal set
            {
                if (_date != value)
                {
                    DateTime oldDate = _date;
                    _date = value;
                    OnPropertyChanged("Date");
                    if (IsDateToday(oldDate) != IsDateToday(_date))
                    {
                        OnPropertyChanged("IsToday");
                    }

                    if (IsDateWeekend(oldDate) != IsDateWeekend(_date))
                    {
                        OnPropertyChanged("IsWeekend");
                    }
                }
            }
        }

        /// <summary>
        /// Indicates whether this selectedDate is a month other than the month displayed in the MonthCalendar control
        /// </summary>
        public bool IsOtherMonth
        {
            get { return _isOtherMonth; }
            internal set
            {
                if (_isOtherMonth != value)
                {
                    _isOtherMonth = value;
                    OnPropertyChanged("IsOtherMonth");
                }
            }
        }

        /// <summary>
        /// Indicates whether this selectedDate should be enabled or not. (This value is used to disable the selectedDate out of Max/MinDate)
        /// </summary>
        public bool IsSelectable
        {
            get { return _isSelectable; }
            internal set
            {
                if (_isSelectable != value)
                {
                    _isSelectable = value;
                    OnPropertyChanged("IsSelectable");
                }
            }
        }

        /// <summary>
        /// Indicates wheter the selectedDate is the same selectedDate specified by the 
        /// </summary>
        public bool IsToday
        {
            get { return IsDateToday(_date); }
        }

        /// <summary>
        /// Indicates whether the selectedDate is a either Saturday or Sunday
        /// </summary>
        public bool IsWeekend
        {
            get { return IsDateWeekend(_date); }
        }

        /// <summary>
        /// 
        /// </summary>
        public object Data
        {
            get { return _data; }
            internal set
            {
                _data = value;
                OnPropertyChanged("Data");
            }
        }

        /// <summary>
        /// Return string representation of this selectedDate
        /// </summary>
        public override string ToString()
        {
            return _date.ToShortDateString();
        }

        #endregion

        #region Internal Properties/Methods

        /// <summary>
        /// Update Date and IsValid property
        /// </summary>
        internal void InternalUpdate(CalendarDate cdate)
        {
            Date = cdate.Date;
            IsOtherMonth = cdate.IsOtherMonth;
            IsSelectable = cdate.IsSelectable;
        }

        #endregion

        private void OnPropertyChanged(String info)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(info));
        }

        private bool IsDateWeekend(DateTime date)
        {
            return date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday;
        }

        private bool IsDateToday(DateTime date)
        {
            return MonthCalendarHelper.CompareYearMonthDay(date, DateTime.Now) == 0;
        }

        private DateTime _date;
        private bool _isOtherMonth;
        private bool _isSelectable = true;
        private object _data;
    }
}

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