■ Binding 태그 확장의 NotifyOnValidationError 속성과 BindingValidationError 이벤트를 사용하는 방법을 보여준다.
▶ UserModel.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<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> |