■ ValidationRule 클래스 사용하기

----------------------------------------------------------------------------------------------------

using System.Globalization;

using System.Windows.Controls;

 

namespace DialogBoxSample

{

    /// <summary>

    /// 마진 검증 규칙

    /// </summary>

    public class MarginValidationRule : ValidationRule

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 최소 마진

        /// </summary>

        private double minimumMargin;

 

        /// <summary>

        /// 최대 마진

        /// </summary>

        private double maximumMargin;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 최소 마진 - MinimumMargin

 

        /// <summary>

        /// 최소 마진

        /// </summary>

        public double MinimumMargin

        {

            get

            {

                return this.minimumMargin;

            }

            set

            {

                this.minimumMargin = value;

            }

        }

 

        #endregion

 

        #region 최대 마진 - MaximumMargin

 

        /// <summary>

        /// 최대 마진

        /// </summary>

        public double MaximumMargin

        {

            get

            {

                return this.maximumMargin;

            }

            set

            {

                this.maximumMargin = value;

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 검증하기 - Validate(value, cultureInfo)

 

        /// <summary>

        /// 검증하기

        /// </summary>

        /// <param name="value"></param>

        /// <param name="cultureInfo">문화 정보</param>

        /// <returns>검증 결과</returns>

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)

        {

            double margin;

 

            if(!double.TryParse((string)value, out margin))

            {

                return new ValidationResult(false, "숫자가 아닙니다.");

            }

 

            if((margin < this.minimumMargin) || (margin > this.maximumMargin))

            {

                string message = string.Format("마진은 {0}과 {1} 사이의 값이어야 합니다.", this.minimumMargin, this.maximumMargin);

 

                return new ValidationResult(false, message);

            }

 

            return new ValidationResult(true, null);

        }

 

        #endregion

    }

}

----------------------------------------------------------------------------------------------------

 

■ XAML 사용하기

----------------------------------------------------------------------------------------------------

...

<TextBox>

    <TextBox.Text>

        <Binding

            Path="Left"

            UpdateSourceTrigger="PropertyChanged">

            <Binding.ValidationRules>

                <local:MarginValidationRule

                    MinimumMargin="0"

                    MaximumMargin="10" />

            </Binding.ValidationRules>

        </Binding>

    </TextBox.Text>

</TextBox>

...

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요