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

回答編集履歴

2

見直しキャンペーン中

2023/07/23 04:34

投稿

TN8001
TN8001

スコア10111

answer CHANGED
@@ -1,120 +1,118 @@
1
- Aは`RelativeSource`を付けることはしません。
2
- コンストラクタで`DataContext = this;`として、
3
- `ItemsSource="{Binding AaaItemsSource}"`
4
- と書きます。
5
- 通常`DataContext = new ViewModel();`、`ViewModel`クラスります。
6
-
7
- Bはそもそもバインドしていません。初期値として値を入れただけです(それで用が足りる場合は問題ありません)
8
- しかし`cb1.ItemsSource = Ccc.Ddd;`とすると**MVVM**から外れることになるので、バインドしてしまうかxamlで固定値を入れることが多いと思います。
9
- ```xaml
10
- <ComboBox>
11
- <sys:String>A</sys:String>
12
- <sys:String>B</sys:String>
13
- <sys:String>C</sys:String>
14
- </ComboBox>
15
- ```
16
-
17
- ---
18
-
19
- `Dictionary`だと本質が見えにくいので、`string`の場合の例です(全く面白くありませんが^^;
20
- 「確認」ボタンを押すと今値を表示します。「倍!!」ボタンを押すと値が2倍に伸びます。
21
-
22
- A`View`から`ViewModel`への変更のみ反映します(`OneWayToSource`)
23
-
24
- Bバインドしていないので何も起きせん。
25
-
26
- Cは双方向に値が反映されま(`TwoWay`)
27
-
28
- [方法: バインディングの方向指定する - WPF | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/framework/wpf/data/how-to-specify-the-direction-of-the-binding)
29
-
30
- ABCどれを選択するかは、要件から自動的に決まります。
31
-
32
- ```xaml
33
- <Window
34
- x:Class="Questions285969.MainWindow"
35
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
36
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
37
- xmlns:sys="clr-namespace:System;assembly=mscorlib"
38
- Width="800"
39
- Height="450">
40
-
41
- <StackPanel>
42
- <!--<ComboBox>
43
- <sys:String>A</sys:String>
44
- <sys:String>B</sys:String>
45
- <sys:String>C</sys:String>
46
- </ComboBox>-->
47
-
48
- <!-- A -->
49
- <TextBox Text="{Binding A}" />
50
-
51
- <!-- B -->
52
- <TextBox x:Name="textBox" />
53
- <!-- 意味的にはただこうしただけ -->
54
- <!--<TextBox Text="B" />-->
55
-
56
- <!-- C -->
57
- <TextBox Text="{Binding C}" />
58
-
59
- <Button Click="Button_Click" Content="確認" />
60
- <Button Click="Button_Click_1" Content="倍!!" />
61
- </StackPanel>
62
- </Window>
63
- ```
64
-
65
- ```C#
66
- using System.ComponentModel;
67
- using System.Runtime.CompilerServices;
68
- using System.Windows;
69
-
70
- namespace Questions285969
71
- {
72
- public partial class MainWindow : Window, INotifyPropertyChanged
73
- {
74
- public string A { get; set; } = "A";
75
-
76
- private string B = "B";
77
-
78
- public string C { get => c; set => Set(ref c, value); }
79
- private string c = "C";
80
-
81
- public MainWindow()
82
- {
83
- InitializeComponent();
84
- DataContext = this;
85
- textBox.Text = B;
86
- }
87
-
88
- private void Button_Click(object sender, RoutedEventArgs e)
89
- {
90
- MessageBox.Show($"A:{A}\nB:{B}\nC:{C}");
91
-
92
- // もちろんこうすれば表示されますが、そういうことはやめましょうというのが「MVVMパターン」です
93
- // (このイベントハンドラ自体MVVMから外れてしまっていますが、そこは本題ではないので^^;
94
- //MessageBox.Show($"A:{A}\nB:{textBox.Text}\nC:{C}");
95
- }
96
-
97
- private void Button_Click_1(object sender, RoutedEventArgs e)
98
- {
99
- A += A;
100
- B += B;
101
- C += C;
102
-
103
- // もちろんこうすれば変わりますが、以下略
104
- //textBox.Text += textBox.Text;
105
- }
106
-
107
-
108
- #region INotifyPropertyChanged
109
- public event PropertyChangedEventHandler PropertyChanged;
110
- protected void Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
111
- {
112
- if(Equals(storage, value)) return;
113
- storage = value;
114
- OnPropertyChanged(propertyName);
115
- }
116
- protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
117
- #endregion
118
- }
119
- }
1
+ Aは`RelativeSource`を付けることはしません。
2
+ コンストラクタで`DataContext = this;`として、`ItemsSource="{Binding AaaItemsSource}"`と書きます。
3
+ 通常は`DataContext = new ViewModel();`と、`ViewModel`クラスを作ります。
4
+
5
+ Bそもそもバインドしていません。初期値して値入れただけです(それで用が足る場合は問題ありせん)
6
+ しかし`cb1.ItemsSource = Ccc.Ddd;`とすると**MVVM**から外れることになるので、バインドしてしまうかxamlで固定値を入れることが多いと思います。
7
+ ```xml
8
+ <ComboBox>
9
+ <sys:String>A</sys:String>
10
+ <sys:String>B</sys:String>
11
+ <sys:String>C</sys:String>
12
+ </ComboBox>
13
+ ```
14
+
15
+ ---
16
+
17
+ `Dictionary`だと本質が見えにくいので、`string`の場合の例です(全く面白くありませんが^^;
18
+ 「確認」ボタンを押すと今の値を表示します。「倍!!」ボタンを押すと値が2倍に伸びます。
19
+
20
+ Aは`View`から`ViewModel`へ変更のみ反映します(`OneWayToSource`)
21
+
22
+ Bバインドていないので何も起きせん。
23
+
24
+ C双方向に値が反映されす(`TwoWay`)
25
+
26
+ [法: バインディングの方を指定る - WPF | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/framework/wpf/data/how-to-specify-the-direction-of-the-binding)
27
+
28
+ ABCどれ選択するかは、要件から自動的に決まります。
29
+
30
+ ```xml
31
+ <Window
32
+ x:Class="Questions285969.MainWindow"
33
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
34
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
35
+ xmlns:sys="clr-namespace:System;assembly=mscorlib"
36
+ Width="800"
37
+ Height="450">
38
+
39
+ <StackPanel>
40
+ <!--<ComboBox>
41
+ <sys:String>A</sys:String>
42
+ <sys:String>B</sys:String>
43
+ <sys:String>C</sys:String>
44
+ </ComboBox>-->
45
+
46
+ <!-- A -->
47
+ <TextBox Text="{Binding A}" />
48
+
49
+ <!-- B -->
50
+ <TextBox x:Name="textBox" />
51
+ <!-- 意味的にはただこうしただけ -->
52
+ <!--<TextBox Text="B" />-->
53
+
54
+ <!-- C -->
55
+ <TextBox Text="{Binding C}" />
56
+
57
+ <Button Click="Button_Click" Content="確認" />
58
+ <Button Click="Button_Click_1" Content="倍!!" />
59
+ </StackPanel>
60
+ </Window>
61
+ ```
62
+
63
+ ```cs
64
+ using System.ComponentModel;
65
+ using System.Runtime.CompilerServices;
66
+ using System.Windows;
67
+
68
+ namespace Questions285969
69
+ {
70
+ public partial class MainWindow : Window, INotifyPropertyChanged
71
+ {
72
+ public string A { get; set; } = "A";
73
+
74
+ private string B = "B";
75
+
76
+ public string C { get => c; set => Set(ref c, value); }
77
+ private string c = "C";
78
+
79
+ public MainWindow()
80
+ {
81
+ InitializeComponent();
82
+ DataContext = this;
83
+ textBox.Text = B;
84
+ }
85
+
86
+ private void Button_Click(object sender, RoutedEventArgs e)
87
+ {
88
+ MessageBox.Show($"A:{A}\nB:{B}\nC:{C}");
89
+
90
+ // もちろんこうすれば表示されますが、そういうことはやめましょうというのが「MVVMパターン」です
91
+ // (このイベントハンドラ自体MVVMから外れてしまっていますが、そこは本題ではないので^^;
92
+ //MessageBox.Show($"A:{A}\nB:{textBox.Text}\nC:{C}");
93
+ }
94
+
95
+ private void Button_Click_1(object sender, RoutedEventArgs e)
96
+ {
97
+ A += A;
98
+ B += B;
99
+ C += C;
100
+
101
+ // もちろんこうすれば変わりますが、以下略
102
+ //textBox.Text += textBox.Text;
103
+ }
104
+
105
+
106
+ #region INotifyPropertyChanged
107
+ public event PropertyChangedEventHandler PropertyChanged;
108
+ protected void Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
109
+ {
110
+ if(Equals(storage, value)) return;
111
+ storage = value;
112
+ OnPropertyChanged(propertyName);
113
+ }
114
+ protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
115
+ #endregion
116
+ }
117
+ }
120
118
  ```

1

修正

2020/08/19 21:45

投稿

TN8001
TN8001

スコア10111

answer CHANGED
@@ -19,11 +19,11 @@
19
19
  `Dictionary`だと本質が見えにくいので、`string`の場合の例です(全く面白くありませんが^^;
20
20
  「確認」ボタンを押すと今の値を表示します。「倍!!」ボタンを押すと値が2倍に伸びます。
21
21
 
22
- Aは`View`から`ViewModel`への変更のみ反映します(`BindingMode.OneWayToSource`)
22
+ Aは`View`から`ViewModel`への変更のみ反映します(`OneWayToSource`)
23
23
 
24
24
  Bはバインドしていないので何も起きません。
25
25
 
26
- Cは双方向に値が反映されます(`BindingMode.TwoWay`)
26
+ Cは双方向に値が反映されます(`TwoWay`)
27
27
 
28
28
  [方法: バインディングの方向を指定する - WPF | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/framework/wpf/data/how-to-specify-the-direction-of-the-binding)
29
29