[C#/SILVERLIGHT] Binding 태그 확장 : NotifyOnValidationError 속성과 BindingValidationError 이벤트 사용하기
C#/Silverlight 2014. 2. 26. 06:37728x90
반응형
728x170
▶ 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
}
728x90
▶ 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
300x250
▶ MainPage.xaml
<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>
728x90
반응형
그리드형(광고전용)
'C# > Silverlight' 카테고리의 다른 글
[C#/SILVERLIGHT] Expression Blend 4 참조 사용하기 (0) | 2014.02.26 |
---|---|
[C#/SILVERLIGHT] ItemsPanelTemplate 엘리먼트 사용하기 (0) | 2014.02.26 |
[C#/SILVERLIGHT] VisualStateManager 엘리먼트 사용하기 (0) | 2014.02.26 |
[C#/SILVERLIGHT] ResourceDictionary 엘리먼트 : MergedDictionaries 속성을 사용해 리소스 파일 병합하기 (0) | 2014.02.26 |
[C#/SILVERLIGHT] 리소스 구하기 (0) | 2014.02.26 |
[C#/SILVERLIGHT] Binding 태그 확장 : NotifyOnValidationError 속성과 BindingValidationError 이벤트 사용하기 (0) | 2014.02.26 |
[C#/SILVERLIGHT] Binding 태그 확장 : INotifyDataErrorInfo 인터페이스를 이용한 바인딩 에러 처리하기 (0) | 2014.02.26 |
[C#/SILVERLIGHT] Binding 태그 확장 : IDataErrorInfo 인터페이스를 이용한 바인딩 에러 처리하기 (0) | 2014.02.25 |
[C#/SILVERLIGHT] Binding 태그 확장 : Exception 객체를 이용한 바인딩 에러 처리하기 (0) | 2014.02.25 |
[C#/SILVERLIGHT] Binding 태그 확장 : FallbackValue 속성 사용하기 (0) | 2014.02.25 |
[C#/SILVERLIGHT] Binding 태그 확장 : TargetNullValue 속성 사용하기 (0) | 2014.02.25 |
댓글을 달아 주세요