回答編集履歴

1

見直しキャンペーン中

2023/07/26 14:12

投稿

TN8001
TN8001

スコア9341

test CHANGED
@@ -1,483 +1,240 @@
1
1
  > Listboxのようなもので、Lableコントロールを共通化できないか?
2
2
 
3
-
3
+ コレクションをバインドできる`ListBox`等を見ると、継承の祖先に`ItemsControl`があることがわかります(選択がなく単に一覧表示したいような場合に最適です)
4
-
5
- コレクションをバインドできる[ListBox クラス (System.Windows.Controls) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.listbox)
4
+ [ListBox クラス (System.Windows.Controls) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.listbox)
6
-
7
- 等を見ると、継承の祖先に[ItemsControl クラス (System.Windows.Controls) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.itemscontrol)
5
+ [ItemsControl クラス (System.Windows.Controls) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.itemscontrol)
8
-
9
- があることがわかります。
10
-
11
- 選択がなく単に一覧表示したいような場合に最適です。
12
-
13
-
14
6
 
15
7
  カスタマイズに関してはこちらが詳しいです。
16
-
17
8
  [ItemsControl 攻略 ~ 外観のカスタマイズ | grabacr.nét](http://grabacr.net/archives/1240)
18
9
 
19
-
10
+ グリッド内の個々のアイテムが同サイズの場合は、`UniformGrid`が便利です。
20
-
21
-
22
-
23
- グリッド内の個々のアイテムが同サイズの場合は、[UniformGrid クラス (System.Windows.Controls.Primitives) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.primitives.uniformgrid)
11
+ [UniformGrid クラス (System.Windows.Controls.Primitives) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.primitives.uniformgrid)
24
-
25
- が便利です。
12
+
26
-
27
-
28
-
29
- しかし並べ方が左右方向にしかできないので`GitHub Contribution Graph`のように上下方向にしたい場合は、
13
+ しかし並べ方が左右方向にしかできないのでGitHub Contribution Graphのように上下方向にしたい場合は、↓がいいんじゃないでしょうか(ざっと試したところ期待通りの動きです)
30
-
31
14
  [Layout orientation of UniformGrid](https://social.msdn.microsoft.com/Forums/en-US/104b9cb8-3e1a-4e2e-ac62-4f9b32cc21f0/layout-orientation-of-uniformgrid?forum=wpf)
32
15
 
33
- こちらがいいんじゃないでしょうか(ざっと試したところ期待通りの動きです)
34
-
35
-
36
-
37
16
  ---
38
17
 
39
-
40
-
41
18
  上記で目的は達成だと思いますが、誤解があるようなので残りの問題も片づけます。
42
19
 
43
-
44
-
45
20
  > ネットの情報によると、Grid.Row、Grid.Columnは効かないらしい・・・
46
21
 
47
-
48
-
49
22
  どちらをご覧になったのかわかりませんが、そんなことないですよ。
50
-
51
23
  「ライブ ビジュアル ツリー」で見るとわかりますが`ContentPresenter`が挟まるため、`ItemContainerStyle`で当てる必要があるのが注意点です。
52
24
 
53
-
54
-
55
25
  [デバッグ中に XAML のプロパティを調べる - Visual Studio | Microsoft Docs](https://docs.microsoft.com/ja-jp/visualstudio/xaml-tools/inspect-xaml-properties-while-debugging)
56
-
57
-
58
-
59
26
  [ItemsControl.ItemContainerStyle プロパティ (System.Windows.Controls) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.itemscontrol.itemcontainerstyle)
60
27
 
61
-
62
-
63
28
  > Labelの内容を1行で記述できるようできないか?
64
29
 
65
-
66
-
67
30
  `DensityToGreenConverter`が、`Color`でなく`Brush`を返せばいいのでは?
68
31
 
69
-
32
+ ---
70
-
33
+
71
- ```xaml
34
+ ```xml
72
-
73
35
  <Window
74
-
75
36
  x:Class="Questions324177.MainWindow"
76
-
77
37
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
78
-
79
38
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
80
-
81
39
  xmlns:local="clr-namespace:Questions324177"
82
-
83
40
  Width="800"
84
-
85
41
  Height="450">
86
-
87
42
  <StackPanel>
88
-
89
43
  <GroupBox Header="UniformGrid">
90
-
91
44
  <ItemsControl HorizontalAlignment="Left" ItemsSource="{Binding Items}">
92
-
93
45
  <ItemsControl.ItemsPanel>
94
-
95
46
  <ItemsPanelTemplate>
96
-
97
47
  <UniformGrid Rows="7" />
98
-
99
48
  <!--<local:UniformGridWithOrientation Rows="7" />-->
100
-
101
49
  </ItemsPanelTemplate>
102
-
103
50
  </ItemsControl.ItemsPanel>
104
-
105
51
  <ItemsControl.ItemTemplate>
106
-
107
52
  <DataTemplate>
108
-
109
53
  <Border
110
-
111
54
  x:Name="border"
112
-
113
55
  Width="10"
114
-
115
56
  Height="10"
116
-
117
57
  Margin="2"
118
-
119
58
  Background="#FFEBEDF0"
120
-
121
59
  CornerRadius="2" />
122
-
123
60
  <DataTemplate.Triggers>
124
-
125
61
  <DataTrigger Binding="{Binding}" Value="1">
126
-
127
62
  <Setter TargetName="border" Property="Background" Value="#FF9BE9A8" />
128
-
129
- </DataTrigger>
63
+ </DataTrigger>
130
-
131
64
  <DataTrigger Binding="{Binding}" Value="2">
132
-
133
65
  <Setter TargetName="border" Property="Background" Value="#FF40C463" />
134
-
135
- </DataTrigger>
66
+ </DataTrigger>
136
-
137
67
  <DataTrigger Binding="{Binding}" Value="3">
138
-
139
68
  <Setter TargetName="border" Property="Background" Value="#FF30A14E" />
140
-
141
- </DataTrigger>
69
+ </DataTrigger>
142
-
143
70
  <DataTrigger Binding="{Binding}" Value="4">
144
-
145
71
  <Setter TargetName="border" Property="Background" Value="#FF216E39" />
146
-
147
- </DataTrigger>
72
+ </DataTrigger>
148
-
149
73
  </DataTemplate.Triggers>
150
-
151
74
  </DataTemplate>
152
-
153
75
  </ItemsControl.ItemTemplate>
154
-
155
76
  </ItemsControl>
156
-
157
77
  </GroupBox>
158
78
 
159
-
160
-
161
79
  <GroupBox Header="Grid Row Column Binding">
162
-
163
80
  <ItemsControl HorizontalAlignment="Left" ItemsSource="{Binding Range}">
164
-
165
81
  <ItemsControl.ItemsPanel>
166
-
167
82
  <ItemsPanelTemplate>
168
-
169
83
  <Grid>
170
-
171
84
  <Grid.ColumnDefinitions>
172
-
173
- <ColumnDefinition />
174
-
175
- <ColumnDefinition />
176
-
177
- <ColumnDefinition />
178
-
179
- <ColumnDefinition />
180
-
181
- <ColumnDefinition />
182
-
183
- <ColumnDefinition />
184
-
185
- <ColumnDefinition />
186
-
187
- <ColumnDefinition />
188
-
189
- <ColumnDefinition />
190
-
191
- <ColumnDefinition />
192
-
193
- <ColumnDefinition />
194
-
195
- <ColumnDefinition />
196
-
197
- <ColumnDefinition />
198
-
199
- <ColumnDefinition />
200
-
201
- <ColumnDefinition />
202
-
203
- <ColumnDefinition />
204
-
205
- <ColumnDefinition />
206
-
207
- <ColumnDefinition />
208
-
209
- <ColumnDefinition />
210
-
211
- <ColumnDefinition />
212
-
213
- <ColumnDefinition />
214
-
215
- <ColumnDefinition />
216
-
217
- <ColumnDefinition />
218
-
219
- <ColumnDefinition />
220
-
221
- <ColumnDefinition />
222
-
223
- <ColumnDefinition />
224
-
225
- <ColumnDefinition />
226
-
227
- <ColumnDefinition />
228
-
229
- <ColumnDefinition />
230
-
231
- <ColumnDefinition />
232
-
233
- <ColumnDefinition />
234
-
235
- <ColumnDefinition />
236
-
237
- <ColumnDefinition />
238
-
239
- <ColumnDefinition />
240
-
241
- <ColumnDefinition />
242
-
243
- <ColumnDefinition />
244
-
245
- <ColumnDefinition />
246
-
247
- <ColumnDefinition />
248
-
249
- <ColumnDefinition />
250
-
251
- <ColumnDefinition />
252
-
253
- <ColumnDefinition />
254
-
255
- <ColumnDefinition />
256
-
257
- <ColumnDefinition />
258
-
259
- <ColumnDefinition />
260
-
261
- <ColumnDefinition />
262
-
263
- <ColumnDefinition />
264
-
265
- <ColumnDefinition />
266
-
267
- <ColumnDefinition />
268
-
269
- <ColumnDefinition />
270
-
271
- <ColumnDefinition />
272
-
273
- <ColumnDefinition />
274
-
275
- <ColumnDefinition />
276
-
277
- <ColumnDefinition />
278
-
85
+ <ColumnDefinition />
86
+ <ColumnDefinition />
87
+ <ColumnDefinition />
88
+ <ColumnDefinition />
89
+ <ColumnDefinition />
90
+ <ColumnDefinition />
91
+ <ColumnDefinition />
92
+ <ColumnDefinition />
93
+ <ColumnDefinition />
94
+ <ColumnDefinition />
95
+ <ColumnDefinition />
96
+ <ColumnDefinition />
97
+ <ColumnDefinition />
98
+ <ColumnDefinition />
99
+ <ColumnDefinition />
100
+ <ColumnDefinition />
101
+ <ColumnDefinition />
102
+ <ColumnDefinition />
103
+ <ColumnDefinition />
104
+ <ColumnDefinition />
105
+ <ColumnDefinition />
106
+ <ColumnDefinition />
107
+ <ColumnDefinition />
108
+ <ColumnDefinition />
109
+ <ColumnDefinition />
110
+ <ColumnDefinition />
111
+ <ColumnDefinition />
112
+ <ColumnDefinition />
113
+ <ColumnDefinition />
114
+ <ColumnDefinition />
115
+ <ColumnDefinition />
116
+ <ColumnDefinition />
117
+ <ColumnDefinition />
118
+ <ColumnDefinition />
119
+ <ColumnDefinition />
120
+ <ColumnDefinition />
121
+ <ColumnDefinition />
122
+ <ColumnDefinition />
123
+ <ColumnDefinition />
124
+ <ColumnDefinition />
125
+ <ColumnDefinition />
126
+ <ColumnDefinition />
127
+ <ColumnDefinition />
128
+ <ColumnDefinition />
129
+ <ColumnDefinition />
130
+ <ColumnDefinition />
131
+ <ColumnDefinition />
132
+ <ColumnDefinition />
133
+ <ColumnDefinition />
134
+ <ColumnDefinition />
135
+ <ColumnDefinition />
136
+ <ColumnDefinition />
137
+ <ColumnDefinition />
279
138
  </Grid.ColumnDefinitions>
280
-
281
139
  <Grid.RowDefinitions>
282
-
283
- <RowDefinition />
140
+ <RowDefinition />
284
-
285
- <RowDefinition />
141
+ <RowDefinition />
286
-
287
- <RowDefinition />
142
+ <RowDefinition />
288
-
289
- <RowDefinition />
143
+ <RowDefinition />
290
-
291
- <RowDefinition />
144
+ <RowDefinition />
292
-
293
- <RowDefinition />
145
+ <RowDefinition />
294
-
295
- <RowDefinition />
146
+ <RowDefinition />
296
-
297
147
  </Grid.RowDefinitions>
298
-
299
148
  </Grid>
300
-
301
149
  </ItemsPanelTemplate>
302
-
303
150
  </ItemsControl.ItemsPanel>
304
-
305
151
  <ItemsControl.ItemContainerStyle>
306
-
307
152
  <Style>
308
-
309
153
  <Setter Property="Grid.Column" Value="{Binding Column}" />
310
-
311
154
  <Setter Property="Grid.Row" Value="{Binding Row}" />
312
-
313
155
  </Style>
314
-
315
156
  </ItemsControl.ItemContainerStyle>
316
-
317
157
  <ItemsControl.ItemTemplate>
318
-
319
158
  <DataTemplate>
320
-
321
159
  <DataTemplate.Resources>
322
-
323
160
  <local:DensityToGreenConverter x:Key="DensityToGreenConverter" />
324
-
325
161
  </DataTemplate.Resources>
326
-
327
162
  <Border
328
-
329
163
  Width="10"
330
-
331
164
  Height="10"
332
-
333
165
  Margin="2"
334
-
335
166
  Background="{Binding Density, Converter={StaticResource DensityToGreenConverter}}"
336
-
337
167
  CornerRadius="2" />
338
-
339
168
  </DataTemplate>
340
-
341
169
  </ItemsControl.ItemTemplate>
342
-
343
170
  </ItemsControl>
344
-
345
171
  </GroupBox>
346
-
347
172
  </StackPanel>
348
-
349
173
  </Window>
350
-
351
174
  ```
352
175
 
353
-
354
-
355
- ```C#
176
+ ```cs
356
-
357
177
  using System;
358
-
359
178
  using System.Globalization;
360
-
361
179
  using System.Linq;
362
-
363
180
  using System.Windows;
364
-
365
181
  using System.Windows.Controls;
366
-
367
182
  using System.Windows.Controls.Primitives;
368
-
369
183
  using System.Windows.Data;
370
-
371
184
  using System.Windows.Media;
372
185
 
373
-
374
-
375
186
  namespace Questions324177
376
-
377
187
  {
378
-
379
188
  public partial class MainWindow : Window
380
-
381
189
  {
382
-
383
190
  public int[] Items { get; }
384
-
385
191
  public Range[] Range { get; }
386
192
 
387
-
388
-
389
193
  public MainWindow()
390
-
391
194
  {
392
-
393
195
  InitializeComponent();
394
-
395
196
  DataContext = this;
396
197
 
397
-
398
-
399
198
  var r = new Random();
400
-
401
199
  Items = Enumerable.Range(0, 366)
402
-
403
200
  .Select(x => r.Next(5))
404
-
405
201
  .ToArray();
406
-
407
202
  Range = Items.Select((v, i) => new Range { Column = i / 7, Row = i % 7, Density = v })
408
-
409
203
  .ToArray();
410
-
411
204
  }
412
-
413
205
  }
414
206
 
415
-
416
-
417
207
  public class Range
418
-
419
208
  {
420
-
421
209
  public int Column { get; set; }
422
-
423
210
  public int Row { get; set; }
424
-
425
211
  public int Density { get; set; }
426
-
427
212
  }
428
213
 
429
-
430
-
431
214
  public class DensityToGreenConverter : IValueConverter // 雑すぎw
432
-
433
215
  {
434
-
435
216
  private static readonly Brush[] brushes;
436
-
437
217
  static DensityToGreenConverter()
438
-
439
218
  {
440
-
441
219
  var c = new BrushConverter();
442
-
443
220
  brushes = new Brush[]
444
-
445
221
  {
446
-
447
222
  (Brush) c.ConvertFrom("#FFEBEDF0"),
448
-
449
223
  (Brush) c.ConvertFrom("#FF9BE9A8"),
450
-
451
224
  (Brush) c.ConvertFrom("#FF40C463"),
452
-
453
225
  (Brush) c.ConvertFrom("#FF30A14E"),
454
-
455
226
  (Brush) c.ConvertFrom("#FF216E39"),
456
-
457
227
  };
458
-
459
228
  }
460
-
461
229
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => brushes[(int)value];
462
-
463
230
  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException();
464
-
465
231
  }
466
232
 
467
-
468
-
469
233
  // 長くなるので省略するが、上下方向に並べたい場合こちらを使用
470
-
471
234
  // [Layout orientation of UniformGrid](https://social.msdn.microsoft.com/Forums/en-US/104b9cb8-3e1a-4e2e-ac62-4f9b32cc21f0/layout-orientation-of-uniformgrid?forum=wpf)
472
-
473
235
  //public class UniformGridWithOrientation : UniformGrid { }
474
-
475
236
  }
476
-
477
237
  ```
478
-
479
238
  ![アプリ画像](fbb268f0469340df0bb91244d7f930a8.png)
480
239
 
481
-
482
-
483
240
  注)元データは同じだが方向が違う。合わせるには`UniformGrid`の代わりに`UniformGridWithOrientation`を使用すること。