App.xaml
<Application x:Class="wp08_personalInfoApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:wp08_personalInfoApp"
StartupUri="Views/MainView.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Light.Green.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Views
MainView.xaml
<mah:MetroWindow
x:Class="wp08_personalInfoApp.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
xmlns:local="clr-namespace:wp08_personalInfoApp.Views"
mc:Ignorable="d"
Title="Personal Info" Height="400" Width="700" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" FontFamily="NanumGothic" >
<mah:MetroWindow.IconTemplate>
<DataTemplate>
<iconPacks:PackIconMaterial Kind="AccountDetails" Width="24" Height="24" Margin="5,5,0,0" Foreground="White"/>
</DataTemplate>
</mah:MetroWindow.IconTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" Margin="10,60">
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="95"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="성" FontSize="15" FontWeight="Bold"
HorizontalAlignment="Right" VerticalAlignment="Center" Margin="20,0"/>
<Label Grid.Row="1" Grid.Column="0" Content="이름" FontSize="15" FontWeight="Bold"
HorizontalAlignment="Right" VerticalAlignment="Center" Margin="20,0"/>
<Label Grid.Row="2" Grid.Column="0" Content="생일" FontSize="15" FontWeight="Bold"
HorizontalAlignment="Right" VerticalAlignment="Center" Margin="20,0"/>
<Label Grid.Row="3" Grid.Column="0" Content="이메일" FontSize="15" FontWeight="Bold"
HorizontalAlignment="Right" VerticalAlignment="Center" Margin="20,0"/>
<Button Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" Margin="100,10" Content="저장"
Style="{StaticResource MahApps.Styles.Button.Square.Accent}"
Command="{Binding ProceedCommand}"/>
<TextBox Grid.Row="0" Grid.Column="1" Margin="10,5" FontSize="15"
Text="{Binding InLastName}"
mah:TextBoxHelper.Watermark="성을 입력하세요"
mah:TextBoxHelper.UseFloatingWatermark="True"
mah:TextBoxHelper.ClearTextButton="True"/>
<TextBox Grid.Row="1" Grid.Column="1" Margin="10,5" FontSize="15"
Text="{Binding InFirstName}"
mah:TextBoxHelper.Watermark="이름을 입력하세요"
mah:TextBoxHelper.UseFloatingWatermark="True"
mah:TextBoxHelper.ClearTextButton="True"/>
<DatePicker Grid.Row="2" Grid.Column="1" Margin="10,5" FontSize="15"
SelectedDate="{Binding InDate , Mode=TwoWay}"
mah:TextBoxHelper.Watermark="생일을 선택하세요"
mah:TextBoxHelper.UseFloatingWatermark="True"
mah:TextBoxHelper.ClearTextButton="True"/>
<TextBox Grid.Row="3" Grid.Column="1" Margin="10,5" FontSize="15"
Text="{Binding InEmail}"
mah:TextBoxHelper.Watermark="이메일을 입력하세요"
mah:TextBoxHelper.UseFloatingWatermark="True"
mah:TextBoxHelper.ClearTextButton="True"/>
</Grid>
<GroupBox Grid.Column="1" Margin="20" Header="결과">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="1" Grid.Column="0" Content="성" FontSize="12" FontWeight="Bold"
HorizontalAlignment="Right" VerticalAlignment="Center" Margin="10,5"/>
<Label Grid.Row="2" Grid.Column="0" Content="이름" FontSize="12" FontWeight="Bold"
HorizontalAlignment="Right" VerticalAlignment="Center" Margin="10,5"/>
<Label Grid.Row="3" Grid.Column="0" Content="이메일" FontSize="12" FontWeight="Bold"
HorizontalAlignment="Right" VerticalAlignment="Center" Margin="10,5"/>
<Label Grid.Row="4" Grid.Column="0" Content="생일" FontSize="12" FontWeight="Bold"
HorizontalAlignment="Right" VerticalAlignment="Center" Margin="10,5"/>
<Label Grid.Row="5" Grid.Column="0" Content="성년" FontSize="12" FontWeight="Bold"
HorizontalAlignment="Right" VerticalAlignment="Center" Margin="10,5"/>
<Label Grid.Row="6" Grid.Column="0" Content="생일여부" FontSize="12" FontWeight="Bold"
HorizontalAlignment="Right" VerticalAlignment="Center" Margin="10,5"/>
<Label Grid.Row="7" Grid.Column="0" Content="12지신" FontSize="12" FontWeight="Bold"
HorizontalAlignment="Right" VerticalAlignment="Center" Margin="10,5"/>
<Label Grid.Row="1" Grid.Column="1" Content="{Binding OutLastName}" FontSize="12"
VerticalAlignment="Center" Margin="10,5"/>
<Label Grid.Row="2" Grid.Column="1" Content="{Binding OutFirstName}" FontSize="12"
VerticalAlignment="Center" Margin="10,5"/>
<Label Grid.Row="3" Grid.Column="1" Content="{Binding OutDate}" FontSize="12"
VerticalAlignment="Center" Margin="10,5"/>
<Label Grid.Row="4" Grid.Column="1" Content="{Binding OutEmail}" FontSize="12"
VerticalAlignment="Center" Margin="10,5"/>
<Label Grid.Row="5" Grid.Column="1" Content="{Binding OutAdult}" FontSize="12"
VerticalAlignment="Center" Margin="10,5"/>
<Label Grid.Row="6" Grid.Column="1" Content="{Binding OutBirthDay}" FontSize="12"
VerticalAlignment="Center" Margin="10,5"/>
<Label Grid.Row="7" Grid.Column="1" Content="{Binding OutZodiac}" FontSize="12"
VerticalAlignment="Center" Margin="10,5"/>
</Grid>
</GroupBox>
</Grid>
</mah:MetroWindow>
MainView.xaml.cs
using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using wp08_personalInfoApp.ViewModels;
namespace wp08_personalInfoApp.Views
{
public partial class MainView : MetroWindow
{
public MainView()
{
InitializeComponent();
this.DataContext = new MainViewModel();
}
}
}
ViewModels
MainViewModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using wp08_personalInfoApp.Models;
namespace wp08_personalInfoApp.ViewModels
{
internal class MainViewModel :ViewModelBase
{
private string inFirstName;
private string inLastName;
private string inEmail;
private DateTime inDate = DateTime.Now;
private string outFirstName;
private string outLastName;
private string outEmail;
private string outDate;
private string outAdult;
private string outBirthDay;
private string outZodiac;
#region <입력을 위한 속성>
public string InFirstName
{
get => inFirstName;
set
{
inFirstName = value;
RaisePropertyChanged(nameof(InFirstName));
}
}
public string InLastName
{
get => inLastName;
set
{
inLastName = value;
RaisePropertyChanged(nameof(inLastName));
}
}
public string InEmail
{
get => inEmail;
set
{
inEmail = value;
RaisePropertyChanged(nameof(InEmail));
}
}
public DateTime InDate
{
get => inDate;
set
{
inDate = value;
RaisePropertyChanged(nameof(InDate));
}
}
#endregion
#region <출력을 위한 속성>
public string OutFirstName
{
get => outFirstName;
set
{
outFirstName = value;
RaisePropertyChanged(nameof(OutFirstName));
}
}
public string OutLastName
{
get => outLastName;
set
{
outLastName = value;
RaisePropertyChanged(nameof(OutLastName));
}
}
public string OutEmail
{
get => outEmail;
set
{
outEmail = value;
RaisePropertyChanged(nameof(OutEmail));
}
}
public string OutDate
{
get => outDate;
set
{
outDate = value;
RaisePropertyChanged(nameof(OutDate));
}
}
public string OutAdult
{
get => outAdult;
set
{
outAdult = value;
RaisePropertyChanged(nameof(OutAdult));
}
}
public string OutBirthDay
{
get => outBirthDay;
set
{
outBirthDay = value;
RaisePropertyChanged(nameof(OutBirthDay));
}
}
public string OutZodiac
{
get => outZodiac;
set
{
outZodiac = value;
RaisePropertyChanged(nameof(OutZodiac));
}
}
#endregion
private ICommand proceedCommand;
public ICommand ProceedCommand
{
get
{
return proceedCommand ?? (proceedCommand = new RelayCommand<object>(
o => Proceed(), o => !string.IsNullOrEmpty(inFirstName) &&
!string.IsNullOrEmpty(inLastName) &&
!string.IsNullOrEmpty(inEmail) &&
!string.IsNullOrEmpty(inDate.ToString())));
}
}
private void Proceed()
{
try
{
Person person = new Person(inFirstName, inLastName, inEmail, inDate);
OutFirstName = person.FirstName;
OutLastName = person.LastName;
OutEmail = person.Email;
OutDate = person.Date.ToString("yyyy년 MM월 dd일");
OutAdult = person.IsAdult == true ? "성년" : "미성년";
OutBirthDay = person.IsBrithDay == true ? "생일" : "-";
OutZodiac = person.Zodiac;
}
catch (Exception ex)
{
MessageBox.Show($"예외발생 {ex.Message}");
}
}
}
}
RelayCommand.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace wp08_personalInfoApp
{
public class RelayCommand<T> : ICommand
{
private readonly Action<T> execute;
private readonly Predicate<T> canExecute;
public event EventHandler CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
}
remove
{
CommandManager.RequerySuggested -= value;
}
}
public bool CanExecute(object parameter)
{
return canExecute?.Invoke((T)parameter) ?? true;
}
public void Execute(object parameter)
{
execute((T)parameter);
}
public RelayCommand(Action<T> execute) : this(execute, null) { }
public RelayCommand(Action<T> execute, Predicate<T> canExecute = null)
{
this.execute = execute ?? throw new ArgumentNullException(nameof(execute));
this.canExecute = canExecute;
}
}
}
ViewModelBase.cs
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace wp08_personalInfoApp.ViewModels
{
internal class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
Models
Person.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using wp08_personalInfoApp.Logics;
namespace wp08_personalInfoApp.Models
{
internal class Person
{
private string email;
private DateTime date;
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email
{
get => email;
set
{
if(Commons.IsValidEmail(value) != true)
{
throw new Exception("유효하지 않은 이메일형식");
}
else
{
email = value;
}
}
}
public DateTime Date
{
get => date;
set
{
var result = Commons.GetAge(value);
if (result > 120 || result <= 0)
{
throw new Exception("유효하지 않은 생일");
}
else
{
date = value;
}
}
}
public bool IsAdult
{
get => Commons.GetAge(date) > 18;
}
public bool IsBrithDay
{
get
{
return DateTime.Now.Month == date.Month &&
DateTime.Now.Day == date.Day;
}
}
public string Zodiac
{
get => Commons.GetZodiac(date);
}
public Person(string firstName, string lastName, string email, DateTime date)
{
FirstName = firstName;
LastName = lastName;
Email = email;
Date = date;
}
}
}
Logics
Commons.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace wp08_personalInfoApp.Logics
{
internal class Commons
{
public static bool IsValidEmail(string email)
{
var strPattern = @"^([0-9a-zA-Z]+)@([0-9a-zA-Z]+)(\.[0-9a-zA-Z]+){1,}$";
return Regex.IsMatch(email, strPattern);
}
public static int GetAge(DateTime value)
{
int result;
if(DateTime.Now.Month < value.Month || DateTime.Now.Month == value.Month &&
DateTime.Now.Day < value.Day)
{
result = DateTime.Now.Year - value.Year - 1;
}
else
{
result = DateTime.Now.Year - value.Year;
}
return result;
}
public static string GetZodiac(DateTime value)
{
int reminder = value.Year % 12;
switch(reminder)
{
case 4:
return "쥐띠";
case 5:
return "소띠";
case 6:
return "호랑이띠";
case 7:
return "토끼띠";
case 8:
return "용띠";
case 9:
return "뱀띠";
case 10:
return "말띠";
case 11:
return "양띠";
case 0:
return "원숭이띠";
case 1:
return "닭띠";
case 2:
return "개띠";
case 3:
return "돼지띠";
default:
return "잡띠";
}
}
}
}
실행결과