xaml
<Window x:Class="G24W1402WPFDialog.GundamDlg"
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:local="clr-namespace:G24W1402WPFDialog"
mc:Ignorable="d"
Title="건담 정보" Height="305" Width="300">
<Border Padding="10">
<DockPanel>
<StackPanel DockPanel.Dock="Top">
<TextBlock
Text="건담 입력"
FontWeight="Bold"
Margin="0, 10"/>
<TextBlock
Text="이름"/>
<TextBox
x:Name="Name2"
Padding="2"/>
<TextBlock
Text="모델"
Margin="0, 10, 0, 0"/>
<TextBox
x:Name="Model"
Padding="2"/>
<TextBlock
Text="소속"
Margin="0, 10, 0, 0"/>
<ComboBox
x:Name="Party"
Padding="2">
<ComboBoxItem>연방군</ComboBoxItem>
<ComboBoxItem>지온군</ComboBoxItem>
<ComboBoxItem>기타</ComboBoxItem>
</ComboBox>
<Button
Content="확인"
Margin="0, 20, 0, 0"
Click="OnOK"/>
<Button
Content="취소"
Margin="0, 20, 0, 0"
Click="OnCancel"/>
</StackPanel>
</DockPanel>
</Border>
</Window>
xaml.cs
using System.Windows;
namespace G24W1402WPFDialog;
/// <summary>
/// GundamDlg.xaml에 대한 상호 작용 논리
/// </summary>
public partial class GundamDlg : Window {
public GundamDlg() {
InitializeComponent();
Name2.Focus();
}
//-----------------------------------
// prop 탭 두 번
//public int MyProperty { get; set; }
//-----------------------------------
//-----------------------------------
// propfull 탭 두 번
//private int myVar;
//public int MyProperty {
// get { return myVar; }
// set { myVar = value; }
//}
//-----------------------------------
// 현재 예제에서는 Control에 있는 Text 값 사용하므로,
// Backing property는 사용하지 않음
//private string _MSName;
public string MSName {
get { return Name2.Text; }
}
public string MSModel => Model.Text;
public string MSParty => Party.Text;
private void OnOK(object sender, RoutedEventArgs e) {
if (string.IsNullOrEmpty(MSName)) {
MessageBox.Show(
"이름을 입력하십시오.",
"입력 부족",
MessageBoxButton.OK,
MessageBoxImage.Warning);
Name2.Focus();
return;
}
if (string.IsNullOrEmpty(MSModel)) {
MessageBox.Show(
"모델을 입력하십시오.",
"입력 부족",
MessageBoxButton.OK,
MessageBoxImage.Warning);
Model.Focus();
return;
}
if (string.IsNullOrEmpty(MSParty)) {
MessageBox.Show(
"소속을 입력하십시오.",
"입력 부족",
MessageBoxButton.OK,
MessageBoxImage.Warning);
Party.Focus();
return;
}
DialogResult = true;
}
private void OnCancel(object sender, RoutedEventArgs e) {
DialogResult = false;
}
}
MVVM 아키텍쳐로 만들기
namespace G24W1402WPFDialog;
//public class GundamModel {
// public string Name { get; set; }
// public string Model { get; set; }
// public string Party { get; set; }
//}
public class GundamModel {
//public GundamModel(string name, string model, string party) {
// Name = name;
// Model = model;
// Party = party;
//}
// Lambda expression 사용
// (name, model, party): Tuple 생성
// Destructuring을 사용하여 각각 Name, Model, Party에 할당
public GundamModel(string name, string model, string party) =>
(Name, Model, Party) = (name, model, party);
// 읽기 전용이므로 생성자에서만 초기화 가능
public string Name { get; }
public string Model { get; }
public string Party { get; }
}
View Model
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
namespace G24W1402WPFDialog;
public class GundamViewModel : INotifyPropertyChanged {
//private string _GundamList = "";
//public string GundamList => _GundamList;
private ObservableCollection<string> _gundamList = new ObservableCollection<string>();
//public ObservableCollection<string> GundamList => _gundamList;
public string GundamListString => string.Join("\n", _gundamList);
public void Add(GundamModel model) {
StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0}의 {1} {2}{3} 추가되었습니다.",
model.Party,
model.Model,
model.Name,
HasJongsung(model.Name) ? "이" : "가");
//_gundamList.Add(sb.ToString()); // 끝에 추가함
_gundamList.Insert(0, sb.ToString()); // 특정 위치에 추가함
// 실제로는 _gundamList를 변경하지만,
// GundamListString이 변경되었다고 알려서 Binding 동작
OnPropertyChanged(nameof(GundamListString));
}
public event PropertyChangedEventHandler? PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propName = "") {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
protected bool HasJongsung(string str) {
if (str.Length < 1)
return true;
char last = str[str.Length - 1];
return (last - 44032) % 28 != 0;
}
}
View
<Window x:Class="G24W1402WPFDialog.MainWindow"
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:local="clr-namespace:G24W1402WPFDialog"
mc:Ignorable="d"
Title="건담" Height="450" Width="800">
<Border Padding="5">
<DockPanel>
<Button
Content="MS 추가"
Margin="10"
Click="OnAdd"
DockPanel.Dock="Top"/>
<TextBox
x:Name="Result"
IsReadOnly="True"
AcceptsReturn="True"
VerticalScrollBarVisibility="Auto"
Background="#eee"
DockPanel.Dock="Bottom"
Text="{Binding GundamListString, Mode=OneWay}"/>
<!--<ScrollViewer DockPanel.Dock="Bottom">
<TextBlock
Background="#eee"
Text="{Binding GundamList}"/>
</ScrollViewer>-->
</DockPanel>
</Border>
</Window>
xaml.cs
using System.Windows;
namespace G24W1402WPFDialog
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public GundamViewModel vm = new GundamViewModel();
public MainWindow()
{
InitializeComponent();
DataContext = vm;
}
public void OnAdd(object sender, RoutedEventArgs e) {
GundamDlg dialog = new GundamDlg();
if (dialog.ShowDialog() != true)
return;
vm.Add(new GundamModel(dialog.MSName, dialog.MSModel, dialog.MSParty));
}
}
}