티스토리 뷰

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(thisnew 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


'C# > WPF' 카테고리의 다른 글

MVVM 예제 - Multiple Views  (0) 2017.07.10
MVVM 예제2 - WPF JSON ListView + MVVM  (0) 2017.07.10
MVVM 예제1  (0) 2017.07.09
WPF tutorial - ListView sorting  (0) 2017.07.04
WPF tutorial - ListView grouping  (0) 2017.07.04
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
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 26 27 28 29 30
글 보관함