teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

変更

2018/03/15 06:47

投稿

anonymousss
anonymousss

スコア13

title CHANGED
File without changes
body CHANGED
@@ -10,8 +10,36 @@
10
10
  どこの実装例を見ても`PropertyChanged`にイベントハンドラは登録されておらずなぜこれで変更の通知ができるのかわからない。
11
11
 
12
12
  ### 該当のソースコード
13
+ 参考ページのソースになります。
14
+ 実際にはFormアプリケーションで`INotifyPropertyChanged`の実装は全く同じ。
15
+ ```C#
16
+ namespace WpfApplication.ViewModels
17
+ using System.ComponentModel;
18
+ using System.Runtime.CompilerServices;
19
+ /// <summary>
20
+ /// ViewModel 基底クラス を表現します。
21
+ /// </summary>
22
+ public class ViewModelBase : INotifyPropertyChanged
23
+ {
24
+ /// <summary>
25
+ /// プロパティ値が変更されたことをクライアントに通知します。
26
+ /// </summary>
27
+ public event PropertyChangedEventHandler PropertyChanged;
28
+ /// <summary>
29
+ /// PropertyChanged イベント を発生させます。
30
+ /// </summary>
31
+ /// <param name="propertyName">変更されたプロパティの名前</param>
32
+ protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
33
+ {
34
+ if (this.PropertyChanged != null)
35
+ {
36
+ this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
37
+ }
38
+ }
39
+ }
40
+ ```
13
41
 
14
- 以下のようにしています。
42
+ 以下のように使用しています。
15
43
  ```
16
44
  public class Form1 : Form, INotifyPropertyChanged
17
45
  {

2

typo

2018/03/15 06:47

投稿

anonymousss
anonymousss

スコア13

title CHANGED
File without changes
body CHANGED
@@ -33,7 +33,7 @@
33
33
  // バインディングソースのついか
34
34
  this.bindingSource.DataSource = currentValue;
35
35
  // テキストボックスにバインドを設定
36
- this.TextBox.DataBinding.Add("Text", currentValue, null);
36
+ this.TextBox.DataBinding.Add("Text", bindingSource, null);
37
37
  // コンボボックス選択値変更イベントハンドラ登録
38
38
  this.ComboBox.SelectedValueChanged += new EventHandler(SelectedValueChanged);
39
39
  }

1

srouce

2018/03/15 06:24

投稿

anonymousss
anonymousss

スコア13

title CHANGED
File without changes
body CHANGED
@@ -10,35 +10,51 @@
10
10
  どこの実装例を見ても`PropertyChanged`にイベントハンドラは登録されておらずなぜこれで変更の通知ができるのかわからない。
11
11
 
12
12
  ### 該当のソースコード
13
+
13
- 参考ページソースなります。
14
+ 以下ようしています。
14
- 実際にはFormアプリケーションで`INotifyPropertyChanged`の実装は全く同じ。
15
- ```C#
15
+ ```
16
- namespace WpfApplication.ViewModels
16
+ public class Form1 : Form, INotifyPropertyChanged
17
17
  {
18
- using System.ComponentModel;
19
- using System.Runtime.CompilerServices;
20
-
21
- /// <summary>
22
- /// ViewModel 基底クラス を表現します。
23
- /// </summary>
24
- public class ViewModelBase : INotifyPropertyChanged
18
+ public event PropertyChangedEventHandler PropertyChanged;
19
+ protected void OnPropertyChanged(string propertyName)
25
- {
20
+ {
26
- /// <summary>
21
+ if (PropertyChanged != null)
27
- /// プロパティ値が変更されたことをクライアントに通知します。
28
- /// </summary>
29
- public event PropertyChangedEventHandler PropertyChanged;
22
+ PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
30
-
23
+ }
24
+
31
- /// <summary>
25
+ public Form1()
32
- /// PropertyChanged イベント を発生させます。
33
- /// </summary>
34
- /// <param name="propertyName">変更されたプロパティの名前</param>
35
- protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
36
- {
26
+ {
27
+ InitializeComponent();
28
+
29
+ // コンボボックスにリストを設定
37
- if (this.PropertyChanged != null)
30
+ this.ComboBox.DataSource = list;
31
+ // リスト以外から選択不可
32
+ this.ComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
33
+ // バインディングソースのついか
34
+ this.bindingSource.DataSource = currentValue;
35
+ // テキストボックスにバインドを設定
36
+ this.TextBox.DataBinding.Add("Text", currentValue, null);
37
+ // コンボボックス選択値変更イベントハンドラ登録
38
+ this.ComboBox.SelectedValueChanged += new EventHandler(SelectedValueChanged);
39
+ }
40
+
41
+ List<string> list = new List<string()
38
- {
42
+ {
43
+ "X",
44
+ "Y"
45
+ };
46
+
47
+ int currentValue
48
+ {
49
+ // コンボボックスの値がXなら0、そうでなければ1をテキストボックスに表示
39
- this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
50
+ get { this.ComboBox.SelectedValue.ToString() == "X" ? 0 : 1; }
40
- }
51
+ }
52
+
53
+ void SelectedValueChanged(object sender, EventArgs e)
54
+ {
55
+ // 変更を通知
56
+ OnPropertyChanged("currentValue");
41
- }
57
+ }
42
- }
43
58
  }
59
+
44
60
  ```