티스토리 뷰

C#/WPF

MVVM 예제1

광그로 2017. 7. 9. 17:15

http://www.gamedevforever.com/25544



MainWindow.xaml - View

1
2
3
4
5
6
7
8
9
10
<Grid>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <Label Name="label" Content="{Binding TestNumber}"
                   VerticalAlignment="Center" HorizontalAlignment="Center" ></Label>
            <Button Name="plusButton" Content="+" Command="{Binding PlusCommand}"
                    VerticalAlignment="Center" HorizontalAlignment="Center" Width="50"></Button>
            <Button Name="minusButton" Content="-" Command="{Binding MinusCommand}"
                    VerticalAlignment="Center" HorizontalAlignment="Center" Width="50"></Button>
        </StackPanel>
    </Grid>
cs
MainWindow.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
namespace WPF_Start
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MainViewModel();
        }
    }
}
cs


MainViewModel.xaml - ViewModel

Property가 변하면 View에 자동으로 반영된다.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
 
using Apparelbase.MVVMBase;
using System.Windows.Input;
namespace WPF_Start
{   
    public class MainViewModel:ObservableObject
    {
        #region TestNumber
        private int testNumber;//실제 값을 저장하는 변수, 일반 변수의 시작은 소문자로 시작
        public int TestNumber//의존프로퍼티, VieweModer을 외부에서 접근할 때, TestNumber를 통하여 값을 설정 및 조회
        {
            get { return this.testNumber; }
            set
            {
                if(this.testNumber != value && value >= && value <=10)
                {
                    this.testNumber = value;
                    this.RaisePropertyChanged("TestNumber");
RaisePropertyChanged : 프로그램에게 Property가 변했음을 알려주는 Method
                }
            }
        }
        #endregion
 
        #region PlusCommand()
        private ICommand plusCommand;
        public ICommand PlusCommand
        {
            get
            {
                return (this.plusCommand) ?? (this.plusCommand = new DelegateCommand(Plus, CanPlus));
            }
        }
 
        private bool CanPlus() //ICommand가 실행 가능한 상황인지 아닌지를 판단
        {
            if (this.TestNumber < 10) {
                return true;
            }
            else {
                return false; }
        }
 
        private void Plus() //실제 주요한 동작을 하는 부분
        {
            this.TestNumber++;
        }
        #endregion
 
        #region MinusCommand()
        private ICommand minusCommand;
        public ICommand MinusCommand
        {
            get
            {
                return (this.minusCommand) ?? (this.minusCommand = new DelegateCommand(Minus, CanMinus));
            }
        }
 
        private bool CanMinus()
        {
            if (this.TestNumber>0return true;
            else return false;
        }
 
        private void Minus()
        {
            this.TestNumber--;
        }
        #endregion
 
    }
}
 
cs


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

MVVM 예제2 - WPF JSON ListView + MVVM  (0) 2017.07.10
MVVM 기본 세팅  (0) 2017.07.09
WPF tutorial - ListView sorting  (0) 2017.07.04
WPF tutorial - ListView grouping  (0) 2017.07.04
WPF tutorial - How-to: ListView with left aligned column names  (0) 2017.07.03
공지사항
최근에 올라온 글
최근에 달린 댓글
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
글 보관함