■ FrameworkElement 클래스 : BindingValidationError 이벤트 사용하기

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

XAML

 

 

<StackPanel

    xmlns:local="clr-namespace:SilverlightApplication1"

    BindingValidationError="stackPanel_BindingValidationError">

    <StackPanel.Resources>

        <local:Bill x:Name="BillKey" />

    </StackPanel.Resources>

    <TextBox x:Name="textBox"

        Margin="10"

        Width="50">

        <TextBox.Text>

            <Binding

                Source="{StaticResource BillKey}"

                Path="Amount"

                Mode="TwoWay"

                ValidatesOnExceptions="true"

                NotifyOnValidationError="true" />

        </TextBox.Text>

    </TextBox>

    <Button

        Width="150"

        Height="50"

        Content="Click To Update Source" />

</StackPanel>

 

 

C#

 

 

using System;

using System.Windows.Controls;

using System.Windows.Media;

 

/// <summary>

/// 청구서

/// </summary>

public class Bill

{

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

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

 

    #region Field

 

    /// <summary>

    /// 금액

    /// </summary>

    private double amount;

 

    #endregion

 

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

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

 

    #region 금액 - Amount

 

    /// <summary>

    /// 금액

    /// </summary>

    public double Amount

    {

        get

        {

            return this.amount;

        }

        set

        {

            if(value < 0d)

            {

                throw new Exception("Amount must be greater than zero.");

            }

 

            this.amount = value;

        }

    }

 

    #endregion

}

 

...

 

#region 스택 패널 바인딩 무결성 에러 발생시 처리하기 - stackPanel_BindingValidationError(sender, e)

 

/// <summary>

/// 스택 패널 바인딩 무결성 에러 발생시 처리하기

/// </summary>

/// <param name="sender">이벤트 발생자</param>

/// <param name="e">이벤트 인자</param>

private void stackPanel_BindingValidationError(object sender, ValidationErrorEventArgs e)

{

    if(e.Action == ValidationErrorEventAction.Added)

    {

        this.textBox.Background = new SolidColorBrush(Colors.Red);

    }

    else if(e.Action == ValidationErrorEventAction.Removed)

    {

        this.textBox.Background = new SolidColorBrush(Colors.White);

    }

}

 

#endregion

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요