回答編集履歴

1

見直しキャンペーン中

2023/07/27 13:39

投稿

TN8001
TN8001

スコア9350

test CHANGED
@@ -1,165 +1,82 @@
1
1
  `ObservableCollection`はアイテムの増減の通知はしますが、個々の中身の変更は関知しません。
2
-
3
2
  [ObservableCollection<T> クラス (System.Collections.ObjectModel) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.collections.objectmodel.observablecollection-1)
4
-
5
3
  [INotifyCollectionChanged インターフェイス (System.Collections.Specialized) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.collections.specialized.inotifycollectionchanged)
6
-
7
-
8
4
 
9
5
  そもそも`ObservableCollection<string>`ですと、ソースへの変更自体されていないですね(よくわかっていませんが^^;
10
6
 
11
-
12
-
13
7
  個々のアイテムのほうに`INotifyPropertyChanged`を実装します。
14
8
 
15
-
16
-
17
- ```xaml
9
+ ```xml
18
-
19
10
  <Window
20
-
21
11
  x:Class="Questions337649.Views.MainWindow"
22
-
23
12
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
24
-
25
13
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
26
-
27
14
  Width="800"
28
-
29
15
  Height="450">
30
-
31
16
  <Grid>
32
-
33
17
  <Grid.ColumnDefinitions>
34
-
35
18
  <ColumnDefinition />
36
-
37
19
  <ColumnDefinition />
38
-
39
20
  </Grid.ColumnDefinitions>
40
-
41
21
  <ListView HorizontalContentAlignment="Stretch" ItemsSource="{Binding Items}">
42
-
43
22
  <ListView.ItemTemplate>
44
-
45
23
  <DataTemplate>
46
-
47
24
  <TextBox Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}" />
48
-
49
25
  </DataTemplate>
50
-
51
26
  </ListView.ItemTemplate>
52
-
53
27
  </ListView>
54
28
 
55
-
56
-
57
29
  <ListView Grid.Column="1" ItemsSource="{Binding Items}">
58
-
59
30
  <ListView.ItemTemplate>
60
-
61
31
  <DataTemplate>
62
-
63
32
  <TextBlock Text="{Binding Text}" />
64
-
65
33
  </DataTemplate>
66
-
67
34
  </ListView.ItemTemplate>
68
-
69
35
  </ListView>
70
-
71
36
  </Grid>
72
-
73
37
  </Window>
74
-
75
38
  ```
76
39
 
77
-
78
-
79
- ```C#
40
+ ```cs
80
-
81
41
  using Prism.Mvvm;
82
-
83
42
  using System.Collections.ObjectModel;
84
43
 
85
-
86
-
87
44
  namespace Questions337649.ViewModels
88
-
89
45
  {
90
-
91
46
  public class MainWindowViewModel : BindableBase
92
-
93
47
  {
94
-
95
48
  public ObservableCollection<Item> Items { get; }
96
49
 
97
-
98
-
99
50
  // アイテムの増減がないのであれば、配列でいいです。
100
-
101
51
  //public Item[] Items { get; }
102
52
 
103
-
104
-
105
53
  public MainWindowViewModel()
106
-
107
54
  {
108
-
109
55
  Items = new ObservableCollection<Item>
110
-
111
56
  {
112
-
113
57
  new Item { Text = "Item1" },
114
-
115
58
  new Item { Text = "Item2" },
116
-
117
59
  new Item { Text = "Item3" },
118
-
119
60
  };
120
61
 
121
-
122
-
123
62
  //Items = new Item[]
124
-
125
63
  //{
126
-
127
64
  // new Item { Text = "Item1" },
128
-
129
65
  // new Item { Text = "Item2" },
130
-
131
66
  // new Item { Text = "Item3" },
132
-
133
67
  //};
134
-
135
68
  }
136
-
137
69
  }
138
70
 
139
-
140
-
141
71
  public class Item : BindableBase
142
-
143
72
  {
144
-
145
73
  private string _Text;
146
-
147
74
  public string Text { get => _Text; set => SetProperty(ref _Text, value); }
148
-
149
75
  }
150
-
151
76
  }
152
-
153
77
  ```
154
-
155
-
156
78
 
157
79
  ---
158
80
 
159
-
160
-
161
81
  回答コードを書いているときに、↓にはまってしまい頭を抱えました^^;(`string Text { get; set; }`で動いてしまった)
162
-
163
-
164
-
165
82
  [wpf - Why does the binding update without implementing INotifyPropertyChanged? - Stack Overflow](https://stackoverflow.com/questions/7767218/why-does-the-binding-update-without-implementing-inotifypropertychanged)