回答編集履歴
2
見直しキャンペーン中
test
CHANGED
@@ -1,517 +1,259 @@
|
|
1
1
|
ちょっと要望と違うのですが、各ItemTypeごとにクラスを分けてしまえばよいのではないでしょうか?(Triggerが手強かったので諦めただけですが^^;
|
2
2
|
|
3
|
-
|
4
|
-
|
5
3
|
文字数制限によりコード削除(編集履歴を参照)
|
6
4
|
|
7
|
-
|
8
|
-
|
9
5
|
---
|
10
6
|
|
11
|
-
|
12
|
-
|
13
7
|
追記
|
14
8
|
|
15
|
-
|
16
|
-
|
17
9
|
## 案1
|
18
|
-
|
19
10
|
バカバカしいが全パターンを入れておいて、`Visibility`で切り替え(ボツでしょうねぇ)
|
20
|
-
|
21
|
-
```x
|
11
|
+
```xml
|
22
|
-
|
23
12
|
<Window
|
24
|
-
|
25
13
|
x:Class="Questions248125.MainWindow"
|
26
|
-
|
27
14
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
28
|
-
|
29
15
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
30
|
-
|
31
16
|
xmlns:local="clr-namespace:Questions248125"
|
32
|
-
|
33
17
|
Width="800"
|
34
|
-
|
35
18
|
Height="450">
|
36
|
-
|
37
19
|
<Grid>
|
38
|
-
|
39
20
|
<ItemsControl
|
40
|
-
|
41
21
|
Background="Black"
|
42
|
-
|
43
22
|
Foreground="White"
|
44
|
-
|
45
23
|
ItemsSource="{Binding Items}">
|
46
|
-
|
47
24
|
<ItemsControl.Resources>
|
48
25
|
|
49
|
-
|
50
|
-
|
51
26
|
<DataTemplate DataType="{x:Type local:Item}">
|
52
|
-
|
53
27
|
<Grid>
|
54
|
-
|
55
28
|
<Line
|
56
|
-
|
57
29
|
Name="Line"
|
58
|
-
|
59
30
|
Stroke="{Binding Brush}"
|
60
|
-
|
61
31
|
StrokeThickness="3"
|
62
|
-
|
63
32
|
X1="0"
|
64
|
-
|
65
33
|
X2="{Binding Width}"
|
66
|
-
|
67
34
|
Y1="0"
|
68
|
-
|
69
35
|
Y2="0" />
|
70
|
-
|
71
36
|
<Rectangle
|
72
|
-
|
73
37
|
Name="Rectangle"
|
74
|
-
|
75
38
|
Width="{Binding Width}"
|
76
|
-
|
77
39
|
Height="{Binding Height}"
|
78
|
-
|
79
40
|
Fill="{Binding Brush}" />
|
80
|
-
|
81
41
|
<Ellipse
|
82
|
-
|
83
42
|
Name="Ellipse"
|
84
|
-
|
85
43
|
Width="{Binding Width}"
|
86
|
-
|
87
44
|
Height="{Binding Height}"
|
88
|
-
|
89
45
|
Fill="{Binding Brush}" />
|
90
|
-
|
91
46
|
</Grid>
|
92
|
-
|
93
47
|
<DataTemplate.Triggers>
|
94
|
-
|
95
48
|
<DataTrigger Binding="{Binding ItemType}" Value="Line">
|
96
|
-
|
97
|
-
<DataTrigger.Setters>
|
49
|
+
<DataTrigger.Setters>
|
98
|
-
|
99
50
|
<Setter TargetName="Line" Property="Visibility" Value="Visible" />
|
100
|
-
|
101
51
|
<Setter TargetName="Rectangle" Property="Visibility" Value="Collapsed" />
|
102
|
-
|
103
52
|
<Setter TargetName="Ellipse" Property="Visibility" Value="Collapsed" />
|
104
|
-
|
105
|
-
</DataTrigger.Setters>
|
53
|
+
</DataTrigger.Setters>
|
106
|
-
|
107
|
-
</DataTrigger>
|
54
|
+
</DataTrigger>
|
108
|
-
|
109
55
|
<DataTrigger Binding="{Binding ItemType}" Value="Rectangle">
|
110
|
-
|
111
|
-
<DataTrigger.Setters>
|
56
|
+
<DataTrigger.Setters>
|
112
|
-
|
113
57
|
<Setter TargetName="Line" Property="Visibility" Value="Collapsed" />
|
114
|
-
|
115
58
|
<Setter TargetName="Rectangle" Property="Visibility" Value="Visible" />
|
116
|
-
|
117
59
|
<Setter TargetName="Ellipse" Property="Visibility" Value="Collapsed" />
|
118
|
-
|
119
|
-
</DataTrigger.Setters>
|
60
|
+
</DataTrigger.Setters>
|
120
|
-
|
121
|
-
</DataTrigger>
|
61
|
+
</DataTrigger>
|
122
|
-
|
123
62
|
<DataTrigger Binding="{Binding ItemType}" Value="Ellipse">
|
124
|
-
|
125
|
-
<DataTrigger.Setters>
|
63
|
+
<DataTrigger.Setters>
|
126
|
-
|
127
64
|
<Setter TargetName="Line" Property="Visibility" Value="Collapsed" />
|
128
|
-
|
129
65
|
<Setter TargetName="Rectangle" Property="Visibility" Value="Collapsed" />
|
130
|
-
|
131
66
|
<Setter TargetName="Ellipse" Property="Visibility" Value="Visible" />
|
132
|
-
|
133
|
-
</DataTrigger.Setters>
|
67
|
+
</DataTrigger.Setters>
|
134
|
-
|
135
|
-
</DataTrigger>
|
68
|
+
</DataTrigger>
|
136
|
-
|
137
69
|
</DataTemplate.Triggers>
|
138
|
-
|
139
70
|
</DataTemplate>
|
140
71
|
|
141
|
-
|
142
|
-
|
143
72
|
</ItemsControl.Resources>
|
144
|
-
|
145
73
|
<ItemsControl.ItemsPanel>
|
146
|
-
|
147
74
|
<ItemsPanelTemplate>
|
148
|
-
|
149
75
|
<Canvas />
|
150
|
-
|
151
76
|
</ItemsPanelTemplate>
|
152
|
-
|
153
77
|
</ItemsControl.ItemsPanel>
|
154
|
-
|
155
78
|
<ItemsControl.ItemContainerStyle>
|
156
|
-
|
157
79
|
<Style>
|
158
|
-
|
159
80
|
<Setter Property="Canvas.Top" Value="{Binding Y}" />
|
160
|
-
|
161
81
|
<Setter Property="Canvas.Left" Value="{Binding X}" />
|
162
|
-
|
163
82
|
</Style>
|
164
|
-
|
165
83
|
</ItemsControl.ItemContainerStyle>
|
166
|
-
|
167
84
|
</ItemsControl>
|
168
85
|
|
169
|
-
|
170
|
-
|
171
86
|
<Button
|
172
|
-
|
173
87
|
HorizontalAlignment="Center"
|
174
|
-
|
175
88
|
VerticalAlignment="Top"
|
176
|
-
|
177
89
|
Click="Button_Click"
|
178
|
-
|
179
90
|
Content="ChangeShapes" />
|
180
|
-
|
181
91
|
</Grid>
|
182
|
-
|
183
92
|
</Window>
|
184
|
-
|
185
93
|
```
|
186
94
|
|
187
|
-
|
188
|
-
|
189
|
-
```
|
95
|
+
```cs
|
190
|
-
|
191
96
|
using System;
|
192
|
-
|
193
97
|
using System.Collections.ObjectModel;
|
194
|
-
|
195
98
|
using System.ComponentModel;
|
196
|
-
|
197
99
|
using System.Runtime.CompilerServices;
|
198
|
-
|
199
100
|
using System.Windows;
|
200
|
-
|
201
101
|
using System.Windows.Media;
|
202
102
|
|
203
|
-
|
204
|
-
|
205
103
|
namespace Questions248125
|
206
|
-
|
207
104
|
{
|
208
|
-
|
209
105
|
public enum ItemType { Line, Rectangle, Ellipse, }
|
210
106
|
|
211
|
-
|
212
|
-
|
213
107
|
public class Item : INotifyPropertyChanged
|
214
|
-
|
215
108
|
{
|
216
|
-
|
217
109
|
public int X { get; }
|
218
|
-
|
219
110
|
public int Y { get; }
|
220
|
-
|
221
111
|
public int Width { get; }
|
222
|
-
|
223
112
|
public int Height { get; }
|
224
|
-
|
225
113
|
public SolidColorBrush Brush { get; }
|
226
|
-
|
227
114
|
public ItemType ItemType { get => _ItemType; set => Set(ref _ItemType, value, null); }
|
228
|
-
|
229
115
|
public ItemType _ItemType;
|
230
116
|
|
231
|
-
|
232
|
-
|
233
117
|
private static readonly Random r = new Random();
|
234
|
-
|
235
118
|
private static readonly Array types = Enum.GetValues(typeof(ItemType));
|
236
119
|
|
237
|
-
|
238
|
-
|
239
120
|
public Item()
|
240
|
-
|
241
|
-
{
|
121
|
+
{
|
242
|
-
|
243
122
|
X = r.Next(700);
|
244
|
-
|
245
123
|
Y = r.Next(350);
|
246
|
-
|
247
124
|
Width = r.Next(400);
|
248
|
-
|
249
125
|
Height = r.Next(300);
|
250
|
-
|
251
126
|
Brush = new SolidColorBrush(Color.FromRgb((byte)r.Next(255), (byte)r.Next(255), (byte)r.Next(255)));
|
252
|
-
|
253
127
|
ItemType = (ItemType)types.GetValue(r.Next(types.Length));
|
254
|
-
|
255
|
-
}
|
128
|
+
}
|
256
|
-
|
257
|
-
|
258
129
|
|
259
130
|
public void ChangeShape() => ItemType = (ItemType)types.GetValue(r.Next(types.Length));
|
260
131
|
|
261
|
-
|
262
|
-
|
263
132
|
#region INotifyPropertyChanged
|
264
|
-
|
265
133
|
public event PropertyChangedEventHandler PropertyChanged;
|
266
|
-
|
267
134
|
protected bool Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
|
268
|
-
|
269
|
-
{
|
135
|
+
{
|
270
|
-
|
271
136
|
if(Equals(storage, value)) return false;
|
272
|
-
|
273
137
|
storage = value;
|
274
|
-
|
275
138
|
OnPropertyChanged(propertyName);
|
276
|
-
|
277
139
|
return true;
|
278
|
-
|
279
|
-
}
|
140
|
+
}
|
280
|
-
|
281
141
|
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
282
|
-
|
283
142
|
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
284
|
-
|
285
143
|
#endregion
|
286
|
-
|
287
144
|
}
|
288
145
|
|
289
|
-
|
290
|
-
|
291
146
|
public partial class MainWindow : Window
|
292
|
-
|
293
147
|
{
|
294
|
-
|
295
148
|
public ObservableCollection<Item> Items { get; }
|
296
149
|
|
297
|
-
|
298
|
-
|
299
150
|
public MainWindow()
|
300
|
-
|
301
|
-
{
|
151
|
+
{
|
302
|
-
|
303
152
|
InitializeComponent();
|
304
153
|
|
305
|
-
|
306
|
-
|
307
154
|
DataContext = this;
|
308
|
-
|
309
155
|
Items = new ObservableCollection<Item>
|
310
|
-
|
311
156
|
{
|
312
|
-
|
313
|
-
new Item(),
|
157
|
+
new Item(),
|
314
|
-
|
315
|
-
new Item(),
|
158
|
+
new Item(),
|
316
|
-
|
317
|
-
new Item(),
|
159
|
+
new Item(),
|
318
|
-
|
319
|
-
new Item(),
|
160
|
+
new Item(),
|
320
|
-
|
321
|
-
new Item(),
|
161
|
+
new Item(),
|
322
|
-
|
323
162
|
};
|
324
|
-
|
325
|
-
}
|
163
|
+
}
|
326
|
-
|
327
164
|
private void Button_Click(object sender, RoutedEventArgs e)
|
328
|
-
|
329
|
-
{
|
165
|
+
{
|
330
|
-
|
331
166
|
foreach(var item in Items) item.ChangeShape();
|
332
|
-
|
333
|
-
}
|
167
|
+
}
|
334
|
-
|
335
168
|
}
|
336
|
-
|
337
169
|
}
|
338
|
-
|
339
170
|
```
|
340
171
|
|
341
|
-
|
342
|
-
|
343
172
|
## 案2
|
344
|
-
|
345
173
|
1段間に挟まってしまうが、`DataTemplate.Trigger`で`ControlTemplate`を切り替える。
|
346
|
-
|
347
|
-
```x
|
174
|
+
```xml
|
348
|
-
|
349
175
|
<Window
|
350
|
-
|
351
176
|
x:Class="Questions248125.MainWindow"
|
352
|
-
|
353
177
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
354
|
-
|
355
178
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
356
|
-
|
357
179
|
xmlns:local="clr-namespace:Questions248125"
|
358
|
-
|
359
180
|
Width="800"
|
360
|
-
|
361
181
|
Height="450">
|
362
|
-
|
363
182
|
<Grid>
|
364
|
-
|
365
183
|
<ItemsControl
|
366
|
-
|
367
184
|
Background="Black"
|
368
|
-
|
369
185
|
Foreground="White"
|
370
|
-
|
371
186
|
ItemsSource="{Binding Items}">
|
372
|
-
|
373
187
|
<ItemsControl.Resources>
|
374
|
-
|
375
188
|
<ControlTemplate x:Key="Line">
|
376
|
-
|
377
189
|
<Line
|
378
|
-
|
379
190
|
Name="Line"
|
380
|
-
|
381
191
|
Stroke="{Binding Brush}"
|
382
|
-
|
383
192
|
StrokeThickness="3"
|
384
|
-
|
385
193
|
X1="0"
|
386
|
-
|
387
194
|
X2="{Binding Width}"
|
388
|
-
|
389
195
|
Y1="0"
|
390
|
-
|
391
196
|
Y2="0" />
|
392
|
-
|
393
197
|
</ControlTemplate>
|
394
|
-
|
395
198
|
<ControlTemplate x:Key="Rectangle">
|
396
|
-
|
397
199
|
<Rectangle
|
398
|
-
|
399
200
|
Name="Rectangle"
|
400
|
-
|
401
201
|
Width="{Binding Width}"
|
402
|
-
|
403
202
|
Height="{Binding Height}"
|
404
|
-
|
405
203
|
Fill="{Binding Brush}" />
|
406
|
-
|
407
204
|
</ControlTemplate>
|
408
|
-
|
409
205
|
<ControlTemplate x:Key="Ellipse">
|
410
|
-
|
411
206
|
<Ellipse
|
412
|
-
|
413
207
|
Name="Ellipse"
|
414
|
-
|
415
208
|
Width="{Binding Width}"
|
416
|
-
|
417
209
|
Height="{Binding Height}"
|
418
|
-
|
419
210
|
Fill="{Binding Brush}" />
|
420
|
-
|
421
211
|
</ControlTemplate>
|
422
|
-
|
423
212
|
<DataTemplate DataType="{x:Type local:Item}">
|
424
|
-
|
425
213
|
<Control Name="Control" />
|
426
|
-
|
427
214
|
<DataTemplate.Triggers>
|
428
|
-
|
429
215
|
<DataTrigger Binding="{Binding ItemType}" Value="Line">
|
430
|
-
|
431
|
-
<DataTrigger.Setters>
|
216
|
+
<DataTrigger.Setters>
|
432
|
-
|
433
217
|
<Setter TargetName="Control" Property="Template" Value="{StaticResource Line}" />
|
434
|
-
|
435
|
-
</DataTrigger.Setters>
|
218
|
+
</DataTrigger.Setters>
|
436
|
-
|
437
|
-
</DataTrigger>
|
219
|
+
</DataTrigger>
|
438
|
-
|
439
220
|
<DataTrigger Binding="{Binding ItemType}" Value="Rectangle">
|
440
|
-
|
441
|
-
<DataTrigger.Setters>
|
221
|
+
<DataTrigger.Setters>
|
442
|
-
|
443
222
|
<Setter TargetName="Control" Property="Template" Value="{StaticResource Rectangle}" />
|
444
|
-
|
445
|
-
</DataTrigger.Setters>
|
223
|
+
</DataTrigger.Setters>
|
446
|
-
|
447
|
-
</DataTrigger>
|
224
|
+
</DataTrigger>
|
448
|
-
|
449
225
|
<DataTrigger Binding="{Binding ItemType}" Value="Ellipse">
|
450
|
-
|
451
|
-
<DataTrigger.Setters>
|
226
|
+
<DataTrigger.Setters>
|
452
|
-
|
453
227
|
<Setter TargetName="Control" Property="Template" Value="{StaticResource Ellipse}" />
|
454
|
-
|
455
|
-
</DataTrigger.Setters>
|
228
|
+
</DataTrigger.Setters>
|
456
|
-
|
457
|
-
</DataTrigger>
|
229
|
+
</DataTrigger>
|
458
|
-
|
459
230
|
</DataTemplate.Triggers>
|
460
|
-
|
461
231
|
</DataTemplate>
|
462
232
|
|
463
|
-
|
464
|
-
|
465
233
|
</ItemsControl.Resources>
|
466
|
-
|
467
234
|
<ItemsControl.ItemsPanel>
|
468
|
-
|
469
235
|
<ItemsPanelTemplate>
|
470
|
-
|
471
236
|
<Canvas />
|
472
|
-
|
473
237
|
</ItemsPanelTemplate>
|
474
|
-
|
475
238
|
</ItemsControl.ItemsPanel>
|
476
|
-
|
477
239
|
<ItemsControl.ItemContainerStyle>
|
478
|
-
|
479
240
|
<Style>
|
480
|
-
|
481
241
|
<Setter Property="Canvas.Top" Value="{Binding Y}" />
|
482
|
-
|
483
242
|
<Setter Property="Canvas.Left" Value="{Binding X}" />
|
484
|
-
|
485
243
|
</Style>
|
486
|
-
|
487
244
|
</ItemsControl.ItemContainerStyle>
|
488
|
-
|
489
245
|
</ItemsControl>
|
490
246
|
|
491
|
-
|
492
|
-
|
493
247
|
<Button
|
494
|
-
|
495
248
|
HorizontalAlignment="Center"
|
496
|
-
|
497
249
|
VerticalAlignment="Top"
|
498
|
-
|
499
250
|
Click="Button_Click"
|
500
|
-
|
501
251
|
Content="ChangeShapes" />
|
502
|
-
|
503
252
|
</Grid>
|
504
|
-
|
505
253
|
</Window>
|
506
|
-
|
507
254
|
```
|
508
|
-
|
509
255
|
C#コードは同じ
|
510
256
|
|
511
|
-
|
512
|
-
|
513
257
|
## 案3
|
514
|
-
|
515
258
|
いっそのこと`DependencyProperty`で`ItemType`を持った専用コントロールを作ってしまう。
|
516
|
-
|
517
259
|
これも方向性が違うのでコードは省略。
|
1
追記
test
CHANGED
@@ -1,6 +1,22 @@
|
|
1
|
-
ちょっと要望と違うのですが、各
|
1
|
+
ちょっと要望と違うのですが、各ItemTypeごとにクラスを分けてしまえばよいのではないでしょうか?(Triggerが手強かったので諦めただけですが^^;
|
2
|
+
|
3
|
+
|
4
|
+
|
2
|
-
|
5
|
+
文字数制限によりコード削除(編集履歴を参照)
|
6
|
+
|
7
|
+
|
8
|
+
|
3
|
-
|
9
|
+
---
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
追記
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
## 案1
|
18
|
+
|
19
|
+
バカバカしいが全パターンを入れておいて、`Visibility`で切り替え(ボツでしょうねぇ)
|
4
20
|
|
5
21
|
```xaml
|
6
22
|
|
@@ -14,8 +30,6 @@
|
|
14
30
|
|
15
31
|
xmlns:local="clr-namespace:Questions248125"
|
16
32
|
|
17
|
-
Title="MainWindow"
|
18
|
-
|
19
33
|
Width="800"
|
20
34
|
|
21
35
|
Height="450">
|
@@ -34,10 +48,336 @@
|
|
34
48
|
|
35
49
|
|
36
50
|
|
37
|
-
<DataTemplate DataType="{x:Type local:
|
51
|
+
<DataTemplate DataType="{x:Type local:Item}">
|
52
|
+
|
53
|
+
<Grid>
|
54
|
+
|
55
|
+
<Line
|
56
|
+
|
57
|
+
Name="Line"
|
58
|
+
|
59
|
+
Stroke="{Binding Brush}"
|
60
|
+
|
61
|
+
StrokeThickness="3"
|
62
|
+
|
63
|
+
X1="0"
|
64
|
+
|
65
|
+
X2="{Binding Width}"
|
66
|
+
|
67
|
+
Y1="0"
|
68
|
+
|
69
|
+
Y2="0" />
|
70
|
+
|
71
|
+
<Rectangle
|
72
|
+
|
73
|
+
Name="Rectangle"
|
74
|
+
|
75
|
+
Width="{Binding Width}"
|
76
|
+
|
77
|
+
Height="{Binding Height}"
|
78
|
+
|
79
|
+
Fill="{Binding Brush}" />
|
80
|
+
|
81
|
+
<Ellipse
|
82
|
+
|
83
|
+
Name="Ellipse"
|
84
|
+
|
85
|
+
Width="{Binding Width}"
|
86
|
+
|
87
|
+
Height="{Binding Height}"
|
88
|
+
|
89
|
+
Fill="{Binding Brush}" />
|
90
|
+
|
91
|
+
</Grid>
|
92
|
+
|
93
|
+
<DataTemplate.Triggers>
|
94
|
+
|
95
|
+
<DataTrigger Binding="{Binding ItemType}" Value="Line">
|
96
|
+
|
97
|
+
<DataTrigger.Setters>
|
98
|
+
|
99
|
+
<Setter TargetName="Line" Property="Visibility" Value="Visible" />
|
100
|
+
|
101
|
+
<Setter TargetName="Rectangle" Property="Visibility" Value="Collapsed" />
|
102
|
+
|
103
|
+
<Setter TargetName="Ellipse" Property="Visibility" Value="Collapsed" />
|
104
|
+
|
105
|
+
</DataTrigger.Setters>
|
106
|
+
|
107
|
+
</DataTrigger>
|
108
|
+
|
109
|
+
<DataTrigger Binding="{Binding ItemType}" Value="Rectangle">
|
110
|
+
|
111
|
+
<DataTrigger.Setters>
|
112
|
+
|
113
|
+
<Setter TargetName="Line" Property="Visibility" Value="Collapsed" />
|
114
|
+
|
115
|
+
<Setter TargetName="Rectangle" Property="Visibility" Value="Visible" />
|
116
|
+
|
117
|
+
<Setter TargetName="Ellipse" Property="Visibility" Value="Collapsed" />
|
118
|
+
|
119
|
+
</DataTrigger.Setters>
|
120
|
+
|
121
|
+
</DataTrigger>
|
122
|
+
|
123
|
+
<DataTrigger Binding="{Binding ItemType}" Value="Ellipse">
|
124
|
+
|
125
|
+
<DataTrigger.Setters>
|
126
|
+
|
127
|
+
<Setter TargetName="Line" Property="Visibility" Value="Collapsed" />
|
128
|
+
|
129
|
+
<Setter TargetName="Rectangle" Property="Visibility" Value="Collapsed" />
|
130
|
+
|
131
|
+
<Setter TargetName="Ellipse" Property="Visibility" Value="Visible" />
|
132
|
+
|
133
|
+
</DataTrigger.Setters>
|
134
|
+
|
135
|
+
</DataTrigger>
|
136
|
+
|
137
|
+
</DataTemplate.Triggers>
|
138
|
+
|
139
|
+
</DataTemplate>
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
</ItemsControl.Resources>
|
144
|
+
|
145
|
+
<ItemsControl.ItemsPanel>
|
146
|
+
|
147
|
+
<ItemsPanelTemplate>
|
148
|
+
|
149
|
+
<Canvas />
|
150
|
+
|
151
|
+
</ItemsPanelTemplate>
|
152
|
+
|
153
|
+
</ItemsControl.ItemsPanel>
|
154
|
+
|
155
|
+
<ItemsControl.ItemContainerStyle>
|
156
|
+
|
157
|
+
<Style>
|
158
|
+
|
159
|
+
<Setter Property="Canvas.Top" Value="{Binding Y}" />
|
160
|
+
|
161
|
+
<Setter Property="Canvas.Left" Value="{Binding X}" />
|
162
|
+
|
163
|
+
</Style>
|
164
|
+
|
165
|
+
</ItemsControl.ItemContainerStyle>
|
166
|
+
|
167
|
+
</ItemsControl>
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
<Button
|
172
|
+
|
173
|
+
HorizontalAlignment="Center"
|
174
|
+
|
175
|
+
VerticalAlignment="Top"
|
176
|
+
|
177
|
+
Click="Button_Click"
|
178
|
+
|
179
|
+
Content="ChangeShapes" />
|
180
|
+
|
181
|
+
</Grid>
|
182
|
+
|
183
|
+
</Window>
|
184
|
+
|
185
|
+
```
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
```C#
|
190
|
+
|
191
|
+
using System;
|
192
|
+
|
193
|
+
using System.Collections.ObjectModel;
|
194
|
+
|
195
|
+
using System.ComponentModel;
|
196
|
+
|
197
|
+
using System.Runtime.CompilerServices;
|
198
|
+
|
199
|
+
using System.Windows;
|
200
|
+
|
201
|
+
using System.Windows.Media;
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
namespace Questions248125
|
206
|
+
|
207
|
+
{
|
208
|
+
|
209
|
+
public enum ItemType { Line, Rectangle, Ellipse, }
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
public class Item : INotifyPropertyChanged
|
214
|
+
|
215
|
+
{
|
216
|
+
|
217
|
+
public int X { get; }
|
218
|
+
|
219
|
+
public int Y { get; }
|
220
|
+
|
221
|
+
public int Width { get; }
|
222
|
+
|
223
|
+
public int Height { get; }
|
224
|
+
|
225
|
+
public SolidColorBrush Brush { get; }
|
226
|
+
|
227
|
+
public ItemType ItemType { get => _ItemType; set => Set(ref _ItemType, value, null); }
|
228
|
+
|
229
|
+
public ItemType _ItemType;
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
private static readonly Random r = new Random();
|
234
|
+
|
235
|
+
private static readonly Array types = Enum.GetValues(typeof(ItemType));
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
public Item()
|
240
|
+
|
241
|
+
{
|
242
|
+
|
243
|
+
X = r.Next(700);
|
244
|
+
|
245
|
+
Y = r.Next(350);
|
246
|
+
|
247
|
+
Width = r.Next(400);
|
248
|
+
|
249
|
+
Height = r.Next(300);
|
250
|
+
|
251
|
+
Brush = new SolidColorBrush(Color.FromRgb((byte)r.Next(255), (byte)r.Next(255), (byte)r.Next(255)));
|
252
|
+
|
253
|
+
ItemType = (ItemType)types.GetValue(r.Next(types.Length));
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
public void ChangeShape() => ItemType = (ItemType)types.GetValue(r.Next(types.Length));
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
#region INotifyPropertyChanged
|
264
|
+
|
265
|
+
public event PropertyChangedEventHandler PropertyChanged;
|
266
|
+
|
267
|
+
protected bool Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
|
268
|
+
|
269
|
+
{
|
270
|
+
|
271
|
+
if(Equals(storage, value)) return false;
|
272
|
+
|
273
|
+
storage = value;
|
274
|
+
|
275
|
+
OnPropertyChanged(propertyName);
|
276
|
+
|
277
|
+
return true;
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
282
|
+
|
283
|
+
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
284
|
+
|
285
|
+
#endregion
|
286
|
+
|
287
|
+
}
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
public partial class MainWindow : Window
|
292
|
+
|
293
|
+
{
|
294
|
+
|
295
|
+
public ObservableCollection<Item> Items { get; }
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
public MainWindow()
|
300
|
+
|
301
|
+
{
|
302
|
+
|
303
|
+
InitializeComponent();
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
DataContext = this;
|
308
|
+
|
309
|
+
Items = new ObservableCollection<Item>
|
310
|
+
|
311
|
+
{
|
312
|
+
|
313
|
+
new Item(),
|
314
|
+
|
315
|
+
new Item(),
|
316
|
+
|
317
|
+
new Item(),
|
318
|
+
|
319
|
+
new Item(),
|
320
|
+
|
321
|
+
new Item(),
|
322
|
+
|
323
|
+
};
|
324
|
+
|
325
|
+
}
|
326
|
+
|
327
|
+
private void Button_Click(object sender, RoutedEventArgs e)
|
328
|
+
|
329
|
+
{
|
330
|
+
|
331
|
+
foreach(var item in Items) item.ChangeShape();
|
332
|
+
|
333
|
+
}
|
334
|
+
|
335
|
+
}
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
```
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
## 案2
|
344
|
+
|
345
|
+
1段間に挟まってしまうが、`DataTemplate.Trigger`で`ControlTemplate`を切り替える。
|
346
|
+
|
347
|
+
```xaml
|
348
|
+
|
349
|
+
<Window
|
350
|
+
|
351
|
+
x:Class="Questions248125.MainWindow"
|
352
|
+
|
353
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
354
|
+
|
355
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
356
|
+
|
357
|
+
xmlns:local="clr-namespace:Questions248125"
|
358
|
+
|
359
|
+
Width="800"
|
360
|
+
|
361
|
+
Height="450">
|
362
|
+
|
363
|
+
<Grid>
|
364
|
+
|
365
|
+
<ItemsControl
|
366
|
+
|
367
|
+
Background="Black"
|
368
|
+
|
369
|
+
Foreground="White"
|
370
|
+
|
371
|
+
ItemsSource="{Binding Items}">
|
372
|
+
|
373
|
+
<ItemsControl.Resources>
|
374
|
+
|
375
|
+
<ControlTemplate x:Key="Line">
|
38
376
|
|
39
377
|
<Line
|
40
378
|
|
379
|
+
Name="Line"
|
380
|
+
|
41
381
|
Stroke="{Binding Brush}"
|
42
382
|
|
43
383
|
StrokeThickness="3"
|
@@ -50,36 +390,78 @@
|
|
50
390
|
|
51
391
|
Y2="0" />
|
52
392
|
|
393
|
+
</ControlTemplate>
|
394
|
+
|
395
|
+
<ControlTemplate x:Key="Rectangle">
|
396
|
+
|
397
|
+
<Rectangle
|
398
|
+
|
399
|
+
Name="Rectangle"
|
400
|
+
|
401
|
+
Width="{Binding Width}"
|
402
|
+
|
403
|
+
Height="{Binding Height}"
|
404
|
+
|
405
|
+
Fill="{Binding Brush}" />
|
406
|
+
|
407
|
+
</ControlTemplate>
|
408
|
+
|
409
|
+
<ControlTemplate x:Key="Ellipse">
|
410
|
+
|
411
|
+
<Ellipse
|
412
|
+
|
413
|
+
Name="Ellipse"
|
414
|
+
|
415
|
+
Width="{Binding Width}"
|
416
|
+
|
417
|
+
Height="{Binding Height}"
|
418
|
+
|
419
|
+
Fill="{Binding Brush}" />
|
420
|
+
|
421
|
+
</ControlTemplate>
|
422
|
+
|
423
|
+
<DataTemplate DataType="{x:Type local:Item}">
|
424
|
+
|
425
|
+
<Control Name="Control" />
|
426
|
+
|
427
|
+
<DataTemplate.Triggers>
|
428
|
+
|
429
|
+
<DataTrigger Binding="{Binding ItemType}" Value="Line">
|
430
|
+
|
431
|
+
<DataTrigger.Setters>
|
432
|
+
|
433
|
+
<Setter TargetName="Control" Property="Template" Value="{StaticResource Line}" />
|
434
|
+
|
435
|
+
</DataTrigger.Setters>
|
436
|
+
|
437
|
+
</DataTrigger>
|
438
|
+
|
439
|
+
<DataTrigger Binding="{Binding ItemType}" Value="Rectangle">
|
440
|
+
|
441
|
+
<DataTrigger.Setters>
|
442
|
+
|
443
|
+
<Setter TargetName="Control" Property="Template" Value="{StaticResource Rectangle}" />
|
444
|
+
|
445
|
+
</DataTrigger.Setters>
|
446
|
+
|
447
|
+
</DataTrigger>
|
448
|
+
|
449
|
+
<DataTrigger Binding="{Binding ItemType}" Value="Ellipse">
|
450
|
+
|
451
|
+
<DataTrigger.Setters>
|
452
|
+
|
453
|
+
<Setter TargetName="Control" Property="Template" Value="{StaticResource Ellipse}" />
|
454
|
+
|
455
|
+
</DataTrigger.Setters>
|
456
|
+
|
457
|
+
</DataTrigger>
|
458
|
+
|
459
|
+
</DataTemplate.Triggers>
|
460
|
+
|
53
461
|
</DataTemplate>
|
54
462
|
|
55
463
|
|
56
464
|
|
57
|
-
<DataTemplate DataType="{x:Type local:Rectangle}">
|
58
|
-
|
59
|
-
<Rectangle
|
60
|
-
|
61
|
-
Width="{Binding Width}"
|
62
|
-
|
63
|
-
Height="{Binding Height}"
|
64
|
-
|
65
|
-
Fill="{Binding Brush}" />
|
66
|
-
|
67
|
-
</DataTemplate>
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
<DataTemplate DataType="{x:Type local:Ellipse}">
|
72
|
-
|
73
|
-
<Ellipse
|
74
|
-
|
75
|
-
Width="{Binding Width}"
|
76
|
-
|
77
|
-
Height="{Binding Height}"
|
78
|
-
|
79
|
-
Fill="{Binding Brush}" />
|
80
|
-
|
81
|
-
</DataTemplate>
|
82
|
-
|
83
465
|
</ItemsControl.Resources>
|
84
466
|
|
85
467
|
<ItemsControl.ItemsPanel>
|
@@ -110,15 +492,13 @@
|
|
110
492
|
|
111
493
|
<Button
|
112
494
|
|
113
|
-
Width="80"
|
114
|
-
|
115
495
|
HorizontalAlignment="Center"
|
116
496
|
|
117
497
|
VerticalAlignment="Top"
|
118
498
|
|
119
499
|
Click="Button_Click"
|
120
500
|
|
121
|
-
Content="
|
501
|
+
Content="ChangeShapes" />
|
122
502
|
|
123
503
|
</Grid>
|
124
504
|
|
@@ -126,120 +506,12 @@
|
|
126
506
|
|
127
507
|
```
|
128
508
|
|
129
|
-
|
509
|
+
C#コードは同じ
|
130
|
-
|
510
|
+
|
511
|
+
|
512
|
+
|
131
|
-
|
513
|
+
## 案3
|
132
|
-
|
133
|
-
|
514
|
+
|
134
|
-
|
135
|
-
using System.Collections.ObjectModel;
|
136
|
-
|
137
|
-
using System.Windows;
|
138
|
-
|
139
|
-
using System.Windows.Media;
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
namespace Questions248125
|
144
|
-
|
145
|
-
{
|
146
|
-
|
147
|
-
public class Line : Item { }
|
148
|
-
|
149
|
-
public class Rectangle : Item { }
|
150
|
-
|
151
|
-
public class Ellipse : Item { }
|
152
|
-
|
153
|
-
public class Item
|
154
|
-
|
155
|
-
{
|
156
|
-
|
157
|
-
public int X { get; }
|
158
|
-
|
159
|
-
public int Y { get; }
|
160
|
-
|
161
|
-
public int Width { get; }
|
162
|
-
|
163
|
-
public int Height { get; }
|
164
|
-
|
165
|
-
public SolidColorBrush Brush { get; }
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
private static readonly Random r = new Random();
|
170
|
-
|
171
|
-
public Item()
|
172
|
-
|
173
|
-
{
|
174
|
-
|
175
|
-
X = r.Next(700);
|
176
|
-
|
177
|
-
Y = r.Next(350);
|
178
|
-
|
179
|
-
Width = r.Next(400);
|
180
|
-
|
181
|
-
Height = r.Next(300);
|
182
|
-
|
183
|
-
Brush = new SolidColorBrush(Color.FromRgb((byte)r.Next(255), (byte)r.Next(255), (byte)r.Next(255)));
|
184
|
-
|
185
|
-
}
|
186
|
-
|
187
|
-
}
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
public partial class MainWindow : Window
|
192
|
-
|
193
|
-
{
|
194
|
-
|
195
|
-
|
515
|
+
いっそのこと`DependencyProperty`で`ItemType`を持った専用コントロールを作ってしまう。
|
196
|
-
|
197
|
-
|
198
|
-
|
516
|
+
|
199
|
-
|
517
|
+
これも方向性が違うのでコードは省略。
|
200
|
-
|
201
|
-
{
|
202
|
-
|
203
|
-
InitializeComponent();
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
DataContext = this;
|
208
|
-
|
209
|
-
Items = new ObservableCollection<Item>
|
210
|
-
|
211
|
-
{
|
212
|
-
|
213
|
-
new Line(),
|
214
|
-
|
215
|
-
new Rectangle(),
|
216
|
-
|
217
|
-
new Ellipse(),
|
218
|
-
|
219
|
-
};
|
220
|
-
|
221
|
-
}
|
222
|
-
|
223
|
-
private void Button_Click(object sender, RoutedEventArgs e)
|
224
|
-
|
225
|
-
{
|
226
|
-
|
227
|
-
switch(new Random().Next(3))
|
228
|
-
|
229
|
-
{
|
230
|
-
|
231
|
-
case 0: Items.Add(new Line()); break;
|
232
|
-
|
233
|
-
case 1: Items.Add(new Rectangle()); break;
|
234
|
-
|
235
|
-
case 2: Items.Add(new Ellipse()); break;
|
236
|
-
|
237
|
-
}
|
238
|
-
|
239
|
-
}
|
240
|
-
|
241
|
-
}
|
242
|
-
|
243
|
-
}
|
244
|
-
|
245
|
-
```
|