WPF 데이터바인딩, MVVM, DataContext, MVVM, DataBinding, INotifyPropertyChanged를 이용한 예제실습
실행화면
n [Helpers\RelayCommand.cs]
using System;
using System.Windows.Input;
namespace MvvmExam.Helpers
{
class RelayCommand : ICommand
{
#region Fields
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion
#region 생성자
public RelayCommand(Action<object> execute) : this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
#endregion
#region ICommand Member
public bool CanExecute(object param)
{
return _canExecute == null ? true : _canExecute(param);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object param)
{
_execute(param);
}
#endregion
}
}
|
n [Model\Emp.cs]
using System.ComponentModel;
namespace MvvmExam.Model
{
class Emp : INotifyPropertyChanged
{
int _Empno;
public int Empno
{
get
{
return _Empno;
}
set
{
_Empno = value;
OnPropertyChanged("Empno");
}
}
string _Ename;
public string Ename
{
get
{
return _Ename;
}
set
{
_Ename = value;
OnPropertyChanged("Ename");
}
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string pname)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(pname));
}
}
}
|
n [ViewModel\ViewModelBase.cs]
using System.ComponentModel;
namespace MvvmExam.ViewModel
{
class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
internal void OnPropertyChanged(string pname)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(pname));
}
}
}
|
n [ViewModel\ViewModelMain.cs]
using System.Collections.ObjectModel;
using MvvmExam.Model;
using MvvmExam.Helpers;
namespace MvvmExam.ViewModel
{
class ViewModelMain : ViewModelBase
{
public ObservableCollection<Emp> Emps { get; set; }
object _SelectedEmp;
public object SelectedEmp
{
get
{
return _SelectedEmp;
}
set
{
_SelectedEmp = value;
OnPropertyChanged("SelectedEmp");
}
}
string _TextProperty;
public string TextProperty
{
get
{
return _TextProperty;
}
set
{
_TextProperty = value;
OnPropertyChanged("TextProperty1");
}
}
public RelayCommand AddEmpCommand { get; set; }
public ViewModelMain()
{
Emps = new ObservableCollection<Emp>()
{
new Emp() { Empno = 1, Ename="공길동"},
new Emp() { Empno = 2, Ename="정길동"},
new Emp() { Empno = 3, Ename="박길동"},
new Emp() { Empno = 4, Ename="윤길동"},
new Emp() { Empno = 5, Ename="김길동"},
new Emp() { Empno = 6, Ename="홍길동"},
};
TextProperty = "이순신";
AddEmpCommand = new RelayCommand(AddEmp);
}
void AddEmp(object param)
{
if (param == null) param = "NAME";
Emps.Add(new Emp() { Empno = Emps.Count+1 , Ename = param.ToString() });
}
}
}
|
n [MainWindow.xaml]
<Window x:Class="MvvmExam.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:MvvmExam.ViewModel"
WindowStartupLocation="CenterScreen"
Title="MainWindow" Height="450" Width="450"
DataContext="{DynamicResource ViewModelMain}">
<Window.Resources>
<vm:ViewModelMain x:Key="ViewModelMain"/>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<GroupBox Header="Classic INotifyPropertyChanged Example"
HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<StackPanel>
<StackPanel Orientation="Horizontal">
<ListBox ItemsSource="{Binding Emps}" SelectedItem="{Binding SelectedEmp}" DisplayMemberPath="Ename" HorizontalAlignment="Left"/>
<DataGrid ItemsSource="{Binding Emps}" SelectedItem="{Binding SelectedEmp}" HorizontalAlignment="Left" Margin="5,0,0,0"/>
<ComboBox ItemsSource="{Binding Emps}" SelectedItem="{Binding SelectedEmp}" DisplayMemberPath="Ename" Margin="5,0,0,5" VerticalAlignment="Top"/>
</StackPanel>
<TextBox x:Name="tb1" Text="{Binding TextProperty, UpdateSourceTrigger=PropertyChanged}" Margin="5"/>
<TextBlock FontWeight="Bold" Margin="5" Text="The TextBox says '"><Run Text="{Binding TextProperty}"/><Run Text="'"/></TextBlock>
</StackPanel>
</ScrollViewer>
<Button Grid.Row="1" Content="Add Emp" Command="{Binding AddEmpCommand}" CommandParameter="{Binding Text, ElementName=tb1}" Margin="5" Focusable="False"/>
</Grid>
</GroupBox>
<Button Content="종료" VerticalAlignment="Top" HorizontalAlignment="Right" FontWeight="Bold" Foreground="Red" Click="Button_Click" Grid.Row="1" Margin="5"/>
</Grid>
</Window>
|
n [MainWindow.xaml.cs]
using System;
using System.Windows;
namespace MvvmExam
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Environment.Exit(0);
//System.Diagnostics.Process.GetCurrentProcess().Kill();
//this.Close();
}
}
}
|
#DataContext, #데이터바인딩, #WPF데이터바인딩, #DataBinding, #WPF, #WPF강좌, #WPF교육, #WPF강의, #시샵, #닷넷, #Csharp, #XAML, #INotifyPropertyChanged