■ Binding 태그 확장에서 IDataErrorInfo 인터페이스를 이용해 바인딩 에러를 처리하는 방법을 보여준다.
▶ Person1.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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
using System.ComponentModel; namespace TestProject { /// <summary> /// 사람 1 /// </summary> public class Person1 : IDataErrorInfo { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 나이 /// </summary> private int age; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 나이 - Age /// <summary> /// 나이 /// </summary> public int Age { get { return this.age; } set { this.age = value; } } #endregion #region 에러 - Error /// <summary> /// 에러 /// </summary> public string Error { get { return string.Empty; } } #endregion #region 인덱서 - this[name] /// <summary> /// 인덱서 /// </summary> /// <param name="name">명칭</param> /// <returns>값</returns> public string this[string name] { get { string result = null; if(name == "Age") { if(this.age < 0 || this.age > 150) { result = "나이는 0보다 적거나 150보다 클 수 없습니다."; } } return result; } } #endregion } } |
▶ Person2.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 |
using System; namespace TestProject { /// <summary> /// 사람 2 /// </summary> public class Person2 { //////////////////////////////////////////////////////////////////////////////////////////////////// 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 < 0 || value > 150) { throw new ArgumentException("나이는 0보다 적거나 150보다 클 수 없습니다."); } this.age = value; } } #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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
<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 태그 확장 : IDataErrorInfo 인터페이스를 이용한 바인딩 에러 처리하기" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <local:Person1 x:Key="Person1Key" /> <local:Person2 x:Key="Person2Key" /> <Style x:Key="ErrorTextBoxStyleKey" TargetType="TextBox"> <Style.Triggers> <Trigger Property="Validation.HasError" Value="true"> <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}" /> </Trigger> </Style.Triggers> </Style> </Window.Resources> <Grid Margin="0"> <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Margin="10"> 나이를 입력해 주시기 바랍니다 : </TextBlock> <TextBox Grid.Row="1" Style="{StaticResource ErrorTextBoxStyleKey}" HorizontalAlignment="Left" Margin="10" Width="150" Height="25" VerticalContentAlignment="Center"> <TextBox.Text> <Binding Source="{StaticResource Person1Key}" Path="Age" ValidatesOnDataErrors="True" UpdateSourceTrigger="PropertyChanged"> </Binding> </TextBox.Text> </TextBox> <TextBlock Grid.Row="2" Margin="10"> 검증 에러 메시지를 보기 위해서 마우스를 위에 위치하시기 바랍니다. </TextBlock> <TextBlock Grid.Row="3" Margin="10"> 나이를 입력해 주시기 바랍니다 : </TextBlock> <TextBox Grid.Row="4" Style="{StaticResource ErrorTextBoxStyleKey}" HorizontalAlignment="Left" Margin="10" Width="150" Height="25" VerticalContentAlignment="Center"> <TextBox.Text> <Binding Path="Age" Source="{StaticResource Person2Key}" ValidatesOnExceptions="True" UpdateSourceTrigger="PropertyChanged"> </Binding> </TextBox.Text> </TextBox> <TextBlock Grid.Row="5" Margin="10"> 검증 에러 메시지를 보기 위해서 마우스를 위에 위치하시기 바랍니다. </TextBlock> </Grid> </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 |
using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); } #endregion } } |