첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ Label 엘리먼트 : Target 속성 사용하기

----------------------------------------------------------------------------------------------------

Customer.cs

 

 

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

 

※ System.Windows.Controls 참조를 추가한다.

※ System.Windows.Controls.Data.Input 참조를 추가한다.

 

 

<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"

            Width="100"

            Height="23"

            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>

</Grid>

 

 

MainPage.xaml.cs

 

 

DataContext = new Customer(12345, "J", "Smith");

 

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요