C#/WPF
MVVM 기본 세팅
광그로
2017. 7. 9. 22:10
1. Model, View, ViewModel로 구분
Model : Data class
View : MainView.xaml
ViewModel : MainViewModel.cs , ViewModelBase.cs
App.xaml
1 2 3 4 5 6 7 8 9 10 11 | <Application x:Class="ListViewSample.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:ListViewSample" DispatcherUnhandledException="Application_DispatcherUnhandledException" Startup="Application_Startup"> <Application.Resources> </Application.Resources> </Application> | cs |
App.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | using ListViewSample.View; using ListViewSample.ViewModel; namespace ListViewSample { /// <summary> /// App.xaml에 대한 상호 작용 논리 /// </summary> public partial class App : Application { private void Application_Startup(object sender, StartupEventArgs e) { MainView mv = new MainView(); mv.DataContext = new MainViewModel(); mv.Show(); } private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { MessageBox.Show(e.Exception.Message); e.Handled = true; } } } | cs |
ViewModelBase.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | using System.ComponentModel; namespace ListViewSample.ViewModel { public abstract class ViewModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void NotifyPropertyChanged(string propertyName) { if(PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } } | cs |
MainView.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | namespace ListViewSample.View { /// <summary> /// MainView.xaml에 대한 상호 작용 논리 /// </summary> public partial class MainView : Window { public MainView() { InitializeComponent(); DataContext = new ViewModel.MainViewModel(); } } } | cs |