質問編集履歴

3

変更

2018/03/15 06:47

投稿

anonymousss
anonymousss

スコア13

test CHANGED
File without changes
test CHANGED
@@ -22,9 +22,65 @@
22
22
 
23
23
  ### 該当のソースコード
24
24
 
25
+ 参考ページのソースになります。
26
+
27
+ 実際にはFormアプリケーションで`INotifyPropertyChanged`の実装は全く同じ。
28
+
29
+ ```C#
30
+
31
+ namespace WpfApplication.ViewModels
32
+
33
+ using System.ComponentModel;
34
+
35
+ using System.Runtime.CompilerServices;
36
+
37
+ /// <summary>
38
+
39
+ /// ViewModel 基底クラス を表現します。
40
+
41
+ /// </summary>
42
+
43
+ public class ViewModelBase : INotifyPropertyChanged
44
+
45
+ {
46
+
47
+ /// <summary>
48
+
49
+ /// プロパティ値が変更されたことをクライアントに通知します。
50
+
51
+ /// </summary>
52
+
53
+ public event PropertyChangedEventHandler PropertyChanged;
54
+
55
+ /// <summary>
56
+
57
+ /// PropertyChanged イベント を発生させます。
58
+
59
+ /// </summary>
60
+
61
+ /// <param name="propertyName">変更されたプロパティの名前</param>
62
+
63
+ protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
64
+
65
+ {
66
+
67
+ if (this.PropertyChanged != null)
68
+
69
+ {
70
+
71
+ this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
72
+
73
+ }
74
+
75
+ }
76
+
77
+ }
78
+
79
+ ```
25
80
 
26
81
 
82
+
27
- 以下のようにしています。
83
+ 以下のように使用しています。
28
84
 
29
85
  ```
30
86
 

2

typo

2018/03/15 06:47

投稿

anonymousss
anonymousss

スコア13

test CHANGED
File without changes
test CHANGED
@@ -68,7 +68,7 @@
68
68
 
69
69
  // テキストボックスにバインドを設定
70
70
 
71
- this.TextBox.DataBinding.Add("Text", currentValue, null);
71
+ this.TextBox.DataBinding.Add("Text", bindingSource, null);
72
72
 
73
73
  // コンボボックス選択値変更イベントハンドラ登録
74
74
 

1

srouce

2018/03/15 06:24

投稿

anonymousss
anonymousss

スコア13

test CHANGED
File without changes
test CHANGED
@@ -22,66 +22,98 @@
22
22
 
23
23
  ### 該当のソースコード
24
24
 
25
- 参考ページのソースになります。
26
25
 
27
- 実際にはFormアプリケーションで`INotifyPropertyChanged`の実装は全く同じ。
28
26
 
29
- ```C#
27
+ 以下のようにしています。
30
28
 
29
+ ```
30
+
31
- namespace WpfApplication.ViewModels
31
+ public class Form1 : Form, INotifyPropertyChanged
32
32
 
33
33
  {
34
34
 
35
- using System.ComponentModel;
35
+ public event PropertyChangedEventHandler PropertyChanged;
36
36
 
37
- using System.Runtime.CompilerServices;
37
+ protected void OnPropertyChanged(string propertyName)
38
38
 
39
-
39
+ {
40
40
 
41
- /// <summary>
41
+ if (PropertyChanged != null)
42
42
 
43
- /// ViewModel 基底クラス を表現します。
43
+ PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
44
44
 
45
- /// </summary>
45
+ }
46
46
 
47
- public class ViewModelBase : INotifyPropertyChanged
48
47
 
49
- {
50
48
 
51
- /// <summary>
49
+ public Form1()
52
50
 
53
- /// プロパティ値が変更されたことをクライアントに通知します。
51
+ {
54
52
 
55
- /// </summary>
53
+ InitializeComponent();
56
54
 
57
- public event PropertyChangedEventHandler PropertyChanged;
58
55
 
59
-
60
56
 
61
- /// <summary>
57
+ // コンボボックスにリストを設定
62
58
 
63
- /// PropertyChanged イベント を発生させます。
59
+ this.ComboBox.DataSource = list;
64
60
 
65
- /// </summary>
61
+ // リスト以外から選択不可
66
62
 
67
- /// <param name="propertyName">変更されたプロパティの名前</param>
63
+ this.ComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
68
64
 
69
- protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
65
+ // バインディングソースのついか
70
66
 
71
- {
67
+ this.bindingSource.DataSource = currentValue;
72
68
 
73
- if (this.PropertyChanged != null)
69
+ // テキストボックスにバインドを設定
74
70
 
75
- {
71
+ this.TextBox.DataBinding.Add("Text", currentValue, null);
76
72
 
77
- this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
73
+ // コンボボックス選択値変更イベントハンドラ登録
78
74
 
79
- }
75
+ this.ComboBox.SelectedValueChanged += new EventHandler(SelectedValueChanged);
80
76
 
81
- }
77
+ }
82
78
 
79
+
80
+
81
+ List<string> list = new List<string()
82
+
83
+ {
84
+
85
+ "X",
86
+
87
+ "Y"
88
+
89
+ };
90
+
91
+
92
+
93
+ int currentValue
94
+
95
+ {
96
+
97
+ // コンボボックスの値がXなら0、そうでなければ1をテキストボックスに表示
98
+
99
+ get { this.ComboBox.SelectedValue.ToString() == "X" ? 0 : 1; }
100
+
83
- }
101
+ }
102
+
103
+
104
+
105
+ void SelectedValueChanged(object sender, EventArgs e)
106
+
107
+ {
108
+
109
+ // 変更を通知
110
+
111
+ OnPropertyChanged("currentValue");
112
+
113
+ }
84
114
 
85
115
  }
86
116
 
117
+
118
+
87
119
  ```