■ Binding 엘리먼트의 ValidationRules 속성을 사용해 바인딩을 검증하는 방법을 보여준다.
▶ NotNullValidationRule.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 |
using System.Globalization; using System.Windows.Controls; namespace TestProject { /// <summary> /// NOT NULL 검증 규칙 /// </summary> public class NotNullValidationRule : ValidationRule { //////////////////////////////////////////////////////////////////////////////////////////////////// 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) { string text = value as string; if(!string.IsNullOrEmpty(text)) { return ValidationResult.ValidResult; } else { return new ValidationResult(false, "값이 null이면 안됩니다."); } } #endregion } } |
▶ MainWindow.xaml
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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="Binding 엘리먼트 : ValidationRules 속성을 사용해 바인딩 검증하기" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <local:DataItem x:Key="DataItemKey" /> </Window.Resources> <Grid Margin="10"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal"> <Label Margin="10" Content="입력값" /> <TextBox Name="textBox" Margin="10" Width="150" Height="25" VerticalContentAlignment="Center"> <TextBox.Text> <Binding Source="{StaticResource DataItemKey}" Path="Value" NotifyOnValidationError="True" UpdateSourceTrigger="PropertyChanged"> <Binding.ValidationRules> <local:NotNullValidationRule ValidatesOnTargetUpdated="True" /> </Binding.ValidationRules> </Binding> </TextBox.Text> </TextBox> </StackPanel> </Grid> </Window> |
▶ MainWindow.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 46 47 48 49 50 51 52 53 54 55 |
using System; using System.Windows; using System.Windows.Controls; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.textBox.AddHandler ( Validation.ErrorEvent, new EventHandler<ValidationErrorEventArgs>(textBox_ValidationError) ); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 텍스트 박스 검증 에러시 처리하기 - textBox_ValidationError(sender, e) /// <summary> /// 텍스트 박스 검증 에러시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void textBox_ValidationError(object sender, ValidationErrorEventArgs e) { if(e.Action == ValidationErrorEventAction.Added) { MessageBox.Show(e.Error.ErrorContent.ToString()); } } #endregion } } |