강의 영상 : https://youtu.be/Orwyaq51MXQ?list=PLxU-iZCqT52Cmj47aKB1T-SxI33YL7rYS
아직까지는 이렇게 쓸 일이 있겠나 싶음
Command 패턴을 이용한 영상까지 봐야 할듯
* 데이터 바인딩 의도 : 데이터와 컨트롤을 연결(Binding)시켜놓고 데이터를 시키면 연결된 컨트롤이 알아서 변하도록 함
1. 모델 클래스(User.cs) - INotifyPeopertyChanged 인터페이스를 구현한다
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace ViewModelTest
{
//모델 클래스 : 데이터를 담고 있는 주체
public class User : INotifyPropertyChanged
{
private string _firstName;
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
RaisePropertyChange();
}
}
private string _lastName;
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
RaisePropertyChange();
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChange([CallerMemberName]string propertyname = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
}
}
}
2. ViewModel 클래스(ViewModel.cs) - 모델 클래스를 상속받고, 초기값 할당
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ViewModelTest
{
class ViewModel : User
{
public ViewModel()
{
FirstName = "KIL-DONG";
LastName = "KIM";
}
}
}
3. UI 구성
<Window x:Class="ViewModelTest.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:ViewModelTest"
mc:Ignorable="d"
Title="MainWindow" Height="205" Width="731">
<Grid HorizontalAlignment="Center" Height="190" VerticalAlignment="Center" Width="731">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label x:Name="label" Content="First Name : " HorizontalAlignment="Center" VerticalAlignment="Center" Width="366" Height="63" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" FontSize="18"/>
<Label x:Name="label1" Content="Last Name : " HorizontalAlignment="Center" Grid.Row="1" VerticalAlignment="Center" Width="366" Height="64" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" FontSize="18"/>
<TextBox x:Name="textBox" Grid.Column="1" HorizontalAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Center" TextAlignment="Center" Width="190" Height="17" Text="{Binding FirstName, Mode=TwoWay}"/>
<TextBlock x:Name="textBlock" Grid.Column="1" HorizontalAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Center" Height="22" Grid.Row="1" Width="190" Text="{Binding LastName}"/>
<Button x:Name="button" Content="보기" Grid.Column="1" HorizontalAlignment="Left" Margin="136,31,0,0" Grid.Row="2" VerticalAlignment="Top" Click="button_Click"/>
<Button x:Name="button1" Content="이름변경" Grid.Column="1" HorizontalAlignment="Left" Margin="242,31,0,0" Grid.Row="2" VerticalAlignment="Top" Click="button1_Click"/>
</Grid>
</Window>
4. 구현 클래스
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;
namespace ViewModelTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ViewModel v = new ViewModel();
this.DataContext = v;
}
private void button_Click(object sender, RoutedEventArgs e)
{
ViewModel v = this.DataContext as ViewModel;
MessageBox.Show(v.LastName + "," + v.FirstName);
}
//이름을 홍길동으로 변경
private void button1_Click(object sender, RoutedEventArgs e)
{
ViewModel v = this.DataContext as ViewModel;
v.LastName = "홍";
v.FirstName = "길동";
}
}
}