質問編集履歴

2

コードの修正

2017/03/24 11:08

投稿

sa_sa
sa_sa

スコア15

test CHANGED
File without changes
test CHANGED
@@ -352,49 +352,7 @@
352
352
 
353
353
  }
354
354
 
355
- }
355
+
356
-
357
-
358
-
359
- public class Person : ObservableCollection<Person>
360
-
361
- {
362
-
363
- public Person(){}
364
-
365
- public Person(string name)
366
-
367
- {
368
-
369
- Name = name;
370
-
371
- }
372
-
373
- private string name;
374
-
375
- public string Name
376
-
377
- {
378
-
379
- get { return name; }
380
-
381
- set { name = value; }
382
-
383
- }
384
-
385
- private Person children;
386
-
387
- public Person Children
388
-
389
- {
390
-
391
- get { return children; }
392
-
393
- set { children = value; }
394
-
395
- }
396
-
397
- }
398
356
 
399
357
  }
400
358
 

1

コードの修正

2017/03/24 11:08

投稿

sa_sa
sa_sa

スコア15

test CHANGED
File without changes
test CHANGED
@@ -38,7 +38,7 @@
38
38
 
39
39
  ###該当のソースコード
40
40
 
41
- ListAddRemoveViewプロジェクト
41
+ TreeViewAddRemoveViewプロジェクト
42
42
 
43
43
  ```ここに言語を入力
44
44
 
@@ -66,7 +66,7 @@
66
66
 
67
67
 
68
68
 
69
- <TreeView Name="treeview" Grid.Row="0" ItemsSource="{Binding People}">
69
+ <TreeView Name="treeview" Grid.Row="0" ItemsSource="{Binding TreeObject.People}">
70
70
 
71
71
  <TreeView.ItemTemplate>
72
72
 
@@ -82,7 +82,7 @@
82
82
 
83
83
 
84
84
 
85
- <Button Grid.Row="1" Name="button" Content="RemoveButton" Command="{Binding RemoveElement}" />
85
+ <Button Grid.Row="1" Name="button" Content="RemoveButton" Command="{Binding Person.ChopCommand}" />
86
86
 
87
87
  </Grid>
88
88
 
@@ -130,7 +130,7 @@
130
130
 
131
131
 
132
132
 
133
- ListAddRemoveViewModelプロジェクト
133
+ TreeViewAddRemoveViewModelプロジェクト
134
134
 
135
135
  ```
136
136
 
@@ -138,9 +138,9 @@
138
138
 
139
139
  --------------------------------------------
140
140
 
141
- using System.Windows.Input;
141
+ using GalaSoft.MvvmLight;
142
-
142
+
143
- using Microsoft.Practices.Prism.Commands;
143
+ using GalaSoft.MvvmLight.Command;
144
144
 
145
145
  using System.Collections.ObjectModel;
146
146
 
@@ -150,10 +150,34 @@
150
150
 
151
151
  {
152
152
 
153
- public class MainWindowViewModel
153
+ public class MainWindowViewModel: ViewModelBase
154
154
 
155
155
  {
156
156
 
157
+ public TreeObject TreeObject { get; private set; }
158
+
159
+ public Person Person { get; private set; }
160
+
161
+
162
+
163
+ public MainWindowViewModel()
164
+
165
+ {
166
+
167
+ TreeObject = new TreeObject();
168
+
169
+ Person = new Person();
170
+
171
+ }
172
+
173
+ }
174
+
175
+
176
+
177
+ public class TreeObject : ViewModelBase
178
+
179
+ {
180
+
157
181
  private ObservableCollection<Person> people;
158
182
 
159
183
  public ObservableCollection<Person> People
@@ -166,41 +190,33 @@
166
190
 
167
191
  }
168
192
 
169
-
170
-
171
- private Person selectedPerson = null;
193
+
172
-
194
+
173
- public Person SelectedPerson
195
+ public TreeObject()
174
-
196
+
175
- {
197
+ {
176
-
177
- get { return selectedPerson; }
198
+
178
-
179
- set{ selectedPerson = value; }
180
-
181
- }
182
-
183
-
184
-
185
- public MainWindowViewModel()
186
-
187
- {
188
-
189
- People = new Person()
199
+ var p = new Person()
190
200
 
191
201
  {
192
202
 
203
+ Children =
204
+
205
+ {
206
+
193
- new Person("Human01")
207
+ new Person("Human01")
194
-
195
- {
196
-
197
- Children=new Person
198
208
 
199
209
  {
200
210
 
211
+ Children =
212
+
213
+ {
214
+
201
- new Person("Human02"),
215
+ new Person("Human02"),
202
-
216
+
203
- new Person("Human03")
217
+ new Person("Human03")
218
+
219
+ }
204
220
 
205
221
  }
206
222
 
@@ -208,27 +224,133 @@
208
224
 
209
225
  };
210
226
 
211
- }
212
-
213
-
214
-
215
- private ICommand removeElement;
216
-
217
- public ICommand RemoveElement
218
-
219
- {
220
-
221
- get { return removeElement = new DelegateCommand(removeMethod); }
222
-
223
- }
224
-
225
- private void removeMethod()
226
-
227
- {
228
-
229
-
230
-
231
- }
227
+ p.SetParent();
228
+
229
+ People = p.Children;
230
+
231
+ }
232
+
233
+ }
234
+
235
+
236
+
237
+ public class Person : ViewModelBase
238
+
239
+ {
240
+
241
+ public Person() { }
242
+
243
+ public Person(string name)
244
+
245
+ {
246
+
247
+ Name = name;
248
+
249
+ }
250
+
251
+ private string name;
252
+
253
+ public string Name
254
+
255
+ {
256
+
257
+ get { return name; }
258
+
259
+ set { name = value; }
260
+
261
+ }
262
+
263
+ public Person Parent { get; set; }
264
+
265
+
266
+
267
+ private ObservableCollection<Person> children = new ObservableCollection<Person>();
268
+
269
+ public ObservableCollection<Person> Children
270
+
271
+ {
272
+
273
+ get { return children; }
274
+
275
+ set { children = value; }
276
+
277
+ }
278
+
279
+
280
+
281
+ /// <summary>
282
+
283
+ /// 親子関係の設定
284
+
285
+ /// </summary>
286
+
287
+ public void SetParent()
288
+
289
+ {
290
+
291
+ foreach (var item in Children)
292
+
293
+ {
294
+
295
+ item.Parent = this;
296
+
297
+ item.SetParent();
298
+
299
+ }
300
+
301
+ }
302
+
303
+
304
+
305
+ public void AddChild(Person p)
306
+
307
+ {
308
+
309
+ this.Children.Add(p);
310
+
311
+ p.Parent = this;
312
+
313
+ }
314
+
315
+
316
+
317
+ private void chop()
318
+
319
+ {
320
+
321
+ if(this.Parent != null)
322
+
323
+ {
324
+
325
+ this.Parent.Children.Remove(this);
326
+
327
+ }
328
+
329
+ //this.Parent?.Children.Remove(this);
330
+
331
+ }
332
+
333
+ #region remove Command
334
+
335
+ /// <summary>
336
+
337
+ /// Gets the remove.
338
+
339
+ /// </summary>
340
+
341
+ public RelayCommand ChopCommand
342
+
343
+ {
344
+
345
+ get { return _chopCommand ?? (_chopCommand = new RelayCommand(() => { chop(); })); }
346
+
347
+ }
348
+
349
+ private RelayCommand _chopCommand;
350
+
351
+ #endregion
352
+
353
+ }
232
354
 
233
355
  }
234
356