첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

▶ Product.cs

using System;

/// <summary>
/// 제품
/// </summary>
public class Product
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Field
    ////////////////////////////////////////////////////////////////////////////////////////// Private

    #region Field

    /// <summary>
    /// ID 에러 메시지
    /// </summary>
    private const string ID_ERROR_MESSAGE = "Value cannot be less than 5.";
    
    /// <summary>
    /// ID 경고 메시지
    /// </summary>
    private const string ID_WARNING_MESSAGE = "Value should not be less than 10.";

    /// <summary>
    /// 명칭 에러 메시지
    /// </summary>
    private const string NAME_ERROR_MESSAGE = "Value must not contain any spaces.";

    /// <summary>
    /// 명칭 경고 메시지
    /// </summary>
    private const string NAME_WARNING_MESSAGE = "Value should be 5 characters or less.";

    /// <summary>
    /// ID
    /// </summary>
    private int id;

    /// <summary>
    /// 명칭
    /// </summary>
    private string name;

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Property
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region ID - ID

    /// <summary>
    /// ID
    /// </summary>
    public int ID
    {
        get
        {
            return this.id;
        }
        set
        {
            if(value < 5)
            {
                throw new Exception(ID_ERROR_MESSAGE);
            }

            if(value < 10)
            {
                throw new Exception(ID_WARNING_MESSAGE);
            }

            if(this.id != value)
            {
                this.id = value;
            }
        }
    }

    #endregion

    #region 명칭 - Name

    /// <summary>
    /// 명칭
    /// </summary>
    public string Name
    {
        get
        {
            return this.name;
        }
        set
        {
            if(value.Contains(" "))
            {
                throw new Exception(NAME_ERROR_MESSAGE);
            }

            if(value.Length > 5)
            {
                throw new Exception(NAME_WARNING_MESSAGE);
            }

            if(this.name != value)
            {
                this.name = value;
            }
        }
    }

    #endregion
}

 

728x90

 

▶ MainPage.xaml.cs

using System.Windows.Controls;
using System.Windows.Input;

...

#region 생성자 - MainPage()

/// <summary>
/// 생성자
/// </summary>
public MainPage()
{
    InitializeComponent();

    this.grid.DataContext = new Product() { ID = 10, Name = "food" };
}

#endregion

...

#region 텍스트 박스 키 다운시 처리하기 - TextBox_KeyDown(sender, e)

/// <summary>
/// 텍스트 박스 키 다운시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
    if(e.Key == Key.Enter)
    {
        (sender as TextBox).GetBindingExpression(TextBox.TextProperty).UpdateSource();
    }
}

#endregion

 

300x250

 

▶ MainPage.xaml

<Grid x:Name="grid"
    xmlns:input="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input"
    Margin="10"
    Background="White">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="200"  />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <TextBlock Grid.Row="0" Grid.Column="1"
        Margin="3"
        FontWeight="Bold"
        Text="Product" />
    <input:Label Grid.Row="1" Grid.Column="0"
        HorizontalAlignment="Right"
        Margin="3"
        Target="{Binding ElementName=idTextBox}" />
    <TextBox x:Name="idTextBox" Grid.Row="1" Grid.Column="1"
        Margin="3"
        MaxLength="5" 
        Text="{Binding ID, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}"
        KeyDown="TextBox_KeyDown" />
    <input:DescriptionViewer Grid.Row="1" Grid.Column="2"
        Description="ID must be greater than 4 and should be greater than 9." />
    <input:Label Grid.Row="2" Grid.Column="0"
        HorizontalAlignment="Right"
        Margin="3"
        Target="{Binding ElementName=nameTextBox}" />
    <TextBox x:Name="nameTextBox" Grid.Row="2" Grid.Column="1"
        Margin="3"
        MaxLength="10" 
        Text="{Binding Name, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}"
        KeyDown="TextBox_KeyDown" />
    <input:DescriptionViewer Grid.Row="2" Grid.Column="2"
        Description="Name must not contain spaces and should be 5 characters or less." />
    <input:ValidationSummary Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2"
        Margin="3" />
</Grid>

 

728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요