■ Binding 태그 확장 : NotifyOnValidationError 속성과 BindingValidationError 이벤트 사용하기

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

UserModel.cs

 

 

using System.ComponentModel;

 

/// <summary>

/// 사용자 모델

/// </summary>

public class UserModel : INotifyPropertyChanged

{

    //////////////////////////////////////////////////////////////////////////////////////////////////// Event

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

 

    #region 속성 변경시 - PropertyChanged

 

    /// <summary>

    /// 속성 변경시

    /// </summary>

    public event PropertyChangedEventHandler PropertyChanged;

 

    #endregion

 

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

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

 

    #region Field

 

    /// <summary>

    /// 나이

    /// </summary>

    private int age;

 

    #endregion

 

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

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

 

    #region 나이 - Age

 

    /// <summary>

    /// 나이

    /// </summary>

    public int Age

    {

        get

        {

            return this.age;

        }

        set

        {

            if(value > 100 || value < 0)

            {

                throw new Exception("나이는 0 이상 100 이하를 입력해 주시기 바랍니다.");

            }

 

            this.age = value;

 

            if(PropertyChanged != null)

            {

                PropertyChanged(this, new PropertyChangedEventArgs("Age"));

            }

        }

    }

 

    #endregion

}

 

 

MainPage.xaml.cs

 

 

using System.Windows.Controls;

 

...

 

#region 생성자 - MainPage()

 

/// <summary>

/// 생성자

/// </summary>

public MainPage()

{

    InitializeComponent();

 

    UserModel userModel = new UserModel();

 

    this.grid.DataContext = userModel;

 

    this.grid.BindingValidationError += grid_BindingValidationError;

}

 

#endregion

 

...

 

#region 그리드 바인딩 무결성 에러시 처리하기 - grid_BindingValidationError(sender, e)

 

/// <summary>

/// 그리드 바인딩 무결성 에러시 처리하기

/// </summary>

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

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

private void grid_BindingValidationError(object sender, ValidationErrorEventArgs e)

{

    this.errorTextBlock.Text = string.Empty;

 

    if(e.Action == ValidationErrorEventAction.Added)

    {

        this.errorTextBlock.Text = e.Error.Exception.Message;

    }

}

 

#endregion

 

 

MainPage.xaml.cs

 

 

<Grid x:Name="grid"

    Width="400"

    Height="300">

    <Rectangle Stroke="Black">

        <Rectangle.Fill>

            <LinearGradientBrush

                StartPoint="0.5 0"

                EndPoint="0.5 1">

                <GradientStop Color="#ff00004f" Offset="0" />

                <GradientStop Color="#ff00408c" Offset="1" />

            </LinearGradientBrush>

        </Rectangle.Fill>

    </Rectangle>

    <StackPanel

        HorizontalAlignment="Center"

        VerticalAlignment="Center"

        Orientation="Vertical">

        <StackPanel

            HorizontalAlignment="Center"

            VerticalAlignment="Center"

            Orientation="Horizontal">

            <TextBlock

                HorizontalAlignment="Center"

                VerticalAlignment="Center"

                Foreground="White"

                Text="나이를 입력하세요" />

            <TextBox

                HorizontalAlignment="Center"

                VerticalAlignment="Center"

                Margin="10 0 0 0"

                Width="100"

                Text="{Binding Age, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=true}" />

            <Button

                HorizontalAlignment="Center"

                VerticalAlignment="Center"

                Margin="10 0 0 0"

                Content="나이 검사" />

        </StackPanel>

        <TextBlock x:Name="errorTextBlock"

            Margin="0 20 0 0"

            Foreground="White" />

    </StackPanel>

</Grid>

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요