■ DescriptionViewer 엘리먼트의 Target 속성을 사용하는 방법을 보여준다.
▶ Customer.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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
using System.ComponentModel.DataAnnotations; /// <summary> /// 고객 /// </summary> public class Customer { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// ID 번호 /// </summary> private int idNumber; /// <summary> /// 이름 /// </summary> private string firstName; /// <summary> /// 성 /// </summary> private string lastName; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID 번호 - IDNumber /// <summary> /// ID 번호 /// </summary> [Display(Name = "ID Number", Description = "Enter an integer between 0 and 99999.")] [Range(0, 99999)] public int IDNumber { get { return this.idNumber; } set { Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "IDNumber" }); this.idNumber = value; } } #endregion #region 이름 - FirstName /// <summary> /// 이름 /// </summary> [Display(Name = "Name", Description = "First Name + Last Name.")] [Required(ErrorMessage = "First Name is required.")] [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Numbers and special characters are not allowed in the name.")] public string FirstName { get { return this.firstName; } set { Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "FirstName" }); this.firstName = value; } } #endregion #region 성 - LastName /// <summary> /// 성 /// </summary> [Required(ErrorMessage = "Last Name is required.")] [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Numbers and special characters are not allowed in the name.")] [StringLength(8, MinimumLength = 3, ErrorMessage = "Last name must be between 3 and 8 characters long.")] public string LastName { get { return this.lastName; } set { Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "LastName" }); this.lastName = value; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Customer(idNumber, firstName, lastName) /// <summary> /// 생성자 /// </summary> /// <param name="idNumber">ID 번호</param> /// <param name="firstName">이름</param> /// <param name="lastName">성</param> public Customer(int idNumber, string firstName, string lastName) { IDNumber = idNumber; FirstName = firstName; LastName = lastName; } #endregion } |
▶ MainPage.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 |
<Grid x:Name="grid" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" Margin="15"> <Grid.ColumnDefinitions> <ColumnDefinition Width="100" /> <ColumnDefinition Width="300" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="35" /> <RowDefinition Height="35" /> <RowDefinition Height="35" /> <RowDefinition Height="100"/> </Grid.RowDefinitions> <sdk:Label Grid.Row="0" Grid.Column="0" Margin="5" IsRequired="True" Content="Date of Birth" /> <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal"> <sdk:DatePicker Height="23" /> <sdk:DescriptionViewer Description="Please enter your date of birth." /> </StackPanel> <sdk:Label Grid.Row="1" Grid.Column="0" Margin="5" Target="{Binding ElementName=idNumberTextBox}" /> <StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal"> <TextBox x:Name="idNumberTextBox" Height="23" Width="100" Text="{Binding IDNumber, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" /> <sdk:DescriptionViewer Target="{Binding ElementName=idNumberTextBox}" /> </StackPanel> <sdk:Label Grid.Row="2" Grid.Column="0" Margin="5" Target="{Binding ElementName=nameStackPanel}" PropertyPath="FirstName" /> <StackPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal"> <StackPanel x:Name="nameStackPanel" Orientation="Horizontal"> <TextBox x:Name="firstNameTextBox" Width="100" Height="23" Text="{Binding FirstName, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" /> <TextBox x:Name="lastNameTextBox" Width="100" Height="23" Text="{Binding LastName, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" /> </StackPanel> <sdk:DescriptionViewer Target="{Binding ElementName=nameStackPanel}" PropertyPath="FirstName" /> </StackPanel> <sdk:ValidationSummary Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" /> </Grid> |
※ System.Windows.Controls 참조를 추가한다.
※ System.Windows.Controls.Data.Input 참조를 추가한다.
▶ MainPage.xaml.cs
1 2 3 |
DataContext = new Customer(12345, "J", "Smith"); |
※ System.Windows.Controls 참조를 추가한다.