回答編集履歴

1

見直しキャンペーン中

2023/07/21 13:50

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,445 +1,223 @@
1
1
  文字数上限で追記出来なかったため別回答にさせてください。
2
2
 
3
-
4
-
5
3
  `ItemsControl`を使った例
6
4
 
7
-
8
-
9
- ```xaml
5
+ ```xml
10
-
11
6
  <Window
12
-
13
7
  x:Class="Questions259221.MainWindow"
14
-
15
8
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
16
-
17
9
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
18
-
19
10
  xmlns:sys="clr-namespace:System;assembly=mscorlib"
20
-
21
11
  SizeToContent="WidthAndHeight">
22
-
23
12
  <StackPanel>
24
-
25
13
  <StackPanel Orientation="Horizontal">
26
-
27
14
  <ComboBox
28
-
29
15
  x:Name="ChangeYEAR"
30
-
31
16
  Width="100"
32
-
33
17
  SelectionChanged="SelectionChanged" />
34
-
35
18
  <ComboBox
36
-
37
19
  x:Name="ChangeMONTH"
38
-
39
20
  Width="100"
40
-
41
21
  SelectionChanged="SelectionChanged" />
42
-
43
22
  <Button
44
-
45
23
  x:Name="Today"
46
-
47
24
  Click="Today_Click"
48
-
49
25
  Content="Today" />
50
-
51
26
  </StackPanel>
52
27
 
53
-
54
-
55
28
  <StackPanel Orientation="Horizontal">
56
29
 
57
-
58
-
59
30
  <!-- 選択したい場合 -->
60
-
61
31
  <ListBox
62
-
63
32
  x:Name="calendarSelector"
64
-
65
33
  Width="300"
66
-
67
34
  AlternationCount="7">
68
-
69
35
  <ItemsControl.Template>
70
-
71
36
  <ControlTemplate TargetType="ItemsControl">
72
-
73
37
  <ControlTemplate.Resources>
74
-
75
38
  <Style TargetType="{x:Type TextBlock}">
76
-
77
39
  <Setter Property="VerticalAlignment" Value="Center" />
78
-
79
40
  <Setter Property="HorizontalAlignment" Value="Center" />
80
-
81
41
  </Style>
82
-
83
42
  </ControlTemplate.Resources>
84
-
85
43
  <Border
86
-
87
44
  Background="Azure"
88
-
89
45
  BorderBrush="Black"
90
-
91
46
  BorderThickness="1">
92
-
93
47
  <StackPanel>
94
-
95
48
  <UniformGrid Margin="10,10,10,0" Columns="7">
96
-
97
49
  <TextBlock Foreground="Red" Text="Sun" />
98
-
99
50
  <TextBlock Text="Mon" />
100
-
101
51
  <TextBlock Text="Tue" />
102
-
103
52
  <TextBlock Text="Wed" />
104
-
105
53
  <TextBlock Text="Thu" />
106
-
107
54
  <TextBlock Text="Fri" />
108
-
109
55
  <TextBlock Foreground="Blue" Text="Sat" />
110
-
111
56
  </UniformGrid>
112
-
113
57
  <ItemsPresenter Margin="10" />
114
-
115
58
  </StackPanel>
116
-
117
59
  </Border>
118
-
119
60
  </ControlTemplate>
120
-
121
61
  </ItemsControl.Template>
122
-
123
62
  <ItemsControl.ItemsPanel>
124
-
125
63
  <ItemsPanelTemplate>
126
-
127
64
  <UniformGrid Columns="7" />
128
-
129
65
  </ItemsPanelTemplate>
130
-
131
66
  </ItemsControl.ItemsPanel>
132
-
133
67
  <ItemsControl.ItemTemplate>
134
-
135
68
  <DataTemplate>
136
-
137
69
  <Label
138
-
139
70
  HorizontalContentAlignment="Center"
140
-
141
71
  VerticalContentAlignment="Center"
142
-
143
72
  Content="{Binding Day}"
144
-
145
73
  Foreground="{Binding Foreground, RelativeSource={RelativeSource AncestorType=ListBoxItem}}" />
146
-
147
74
  </DataTemplate>
148
-
149
75
  </ItemsControl.ItemTemplate>
150
-
151
76
  <ItemsControl.ItemContainerStyle>
152
-
153
77
  <Style TargetType="ListBoxItem">
154
-
155
78
  <Setter Property="HorizontalContentAlignment" Value="Stretch" />
156
-
157
79
  <Setter Property="VerticalContentAlignment" Value="Stretch" />
158
-
159
80
  <Style.Triggers>
160
-
161
81
  <Trigger Property="ItemsControl.AlternationIndex" Value="0">
162
-
163
82
  <Setter Property="Foreground" Value="Red" />
164
-
165
- </Trigger>
83
+ </Trigger>
166
-
167
84
  <Trigger Property="ItemsControl.AlternationIndex" Value="6">
168
-
169
85
  <Setter Property="Foreground" Value="Blue" />
170
-
171
- </Trigger>
86
+ </Trigger>
172
-
173
87
  <DataTrigger Binding="{Binding}" Value="{x:Static sys:DateTime.MinValue}">
174
-
175
88
  <Setter Property="Visibility" Value="Hidden" />
176
-
177
89
  </DataTrigger>
178
-
179
90
  </Style.Triggers>
180
-
181
91
  </Style>
182
-
183
92
  </ItemsControl.ItemContainerStyle>
184
-
185
93
  </ListBox>
186
94
 
187
-
188
-
189
95
  <!-- 表示だけの場合 -->
190
-
191
96
  <ItemsControl
192
-
193
97
  x:Name="calendarView"
194
-
195
98
  Width="300"
196
-
197
99
  AlternationCount="7">
198
-
199
100
  <ItemsControl.Template>
200
-
201
101
  <ControlTemplate TargetType="ItemsControl">
202
-
203
102
  <ControlTemplate.Resources>
204
-
205
103
  <Style TargetType="{x:Type TextBlock}">
206
-
207
104
  <Setter Property="VerticalAlignment" Value="Center" />
208
-
209
105
  <Setter Property="HorizontalAlignment" Value="Center" />
210
-
211
106
  </Style>
212
-
213
107
  </ControlTemplate.Resources>
214
-
215
108
  <Border
216
-
217
109
  Background="Azure"
218
-
219
110
  BorderBrush="Black"
220
-
221
111
  BorderThickness="1">
222
-
223
112
  <StackPanel>
224
-
225
113
  <UniformGrid Margin="10,10,10,0" Columns="7">
226
-
227
114
  <TextBlock Foreground="Red" Text="Sun" />
228
-
229
115
  <TextBlock Text="Mon" />
230
-
231
116
  <TextBlock Text="Tue" />
232
-
233
117
  <TextBlock Text="Wed" />
234
-
235
118
  <TextBlock Text="Thu" />
236
-
237
119
  <TextBlock Text="Fri" />
238
-
239
120
  <TextBlock Foreground="Blue" Text="Sat" />
240
-
241
121
  </UniformGrid>
242
-
243
122
  <ItemsPresenter Margin="10" />
244
-
245
123
  </StackPanel>
246
-
247
124
  </Border>
248
-
249
125
  </ControlTemplate>
250
-
251
126
  </ItemsControl.Template>
252
-
253
127
  <ItemsControl.ItemsPanel>
254
-
255
128
  <ItemsPanelTemplate>
256
-
257
129
  <UniformGrid Columns="7" />
258
-
259
130
  </ItemsPanelTemplate>
260
-
261
131
  </ItemsControl.ItemsPanel>
262
-
263
132
  <ItemsControl.ItemTemplate>
264
-
265
133
  <DataTemplate>
266
-
267
134
  <Label
268
-
269
135
  HorizontalContentAlignment="Center"
270
-
271
136
  VerticalContentAlignment="Center"
272
-
273
137
  Content="{Binding Day}"
274
-
275
138
  Foreground="{Binding Tag, RelativeSource={RelativeSource AncestorType=ContentPresenter}}" />
276
-
277
139
  </DataTemplate>
278
-
279
140
  </ItemsControl.ItemTemplate>
280
-
281
141
  <ItemsControl.ItemContainerStyle>
282
-
283
142
  <Style TargetType="ContentPresenter">
284
-
285
143
  <Setter Property="Tag" Value="Black" />
286
-
287
144
  <Style.Triggers>
288
-
289
145
  <Trigger Property="ItemsControl.AlternationIndex" Value="0">
290
-
291
146
  <Setter Property="Tag" Value="Red" />
292
-
293
- </Trigger>
147
+ </Trigger>
294
-
295
148
  <Trigger Property="ItemsControl.AlternationIndex" Value="6">
296
-
297
149
  <Setter Property="Tag" Value="Blue" />
298
-
299
- </Trigger>
150
+ </Trigger>
300
-
301
151
  <DataTrigger Binding="{Binding}" Value="{x:Static sys:DateTime.MinValue}">
302
-
303
152
  <Setter Property="Visibility" Value="Hidden" />
304
-
305
153
  </DataTrigger>
306
-
307
154
  </Style.Triggers>
308
-
309
155
  </Style>
310
-
311
156
  </ItemsControl.ItemContainerStyle>
312
-
313
157
  </ItemsControl>
314
158
 
315
-
316
-
317
159
  <!-- カレンダーを出したいだけなら -->
318
-
319
160
  <Viewbox Width="300">
320
-
321
161
  <Calendar x:Name="calendar" IsHitTestVisible="False" />
322
-
323
162
  </Viewbox>
324
-
325
163
  </StackPanel>
326
164
 
327
-
328
-
329
165
  <TextBlock Text="{Binding SelectedItem, ElementName=calendarSelector, StringFormat=yyyy年MM月dd日}" />
330
166
 
331
-
332
-
333
167
  </StackPanel>
334
-
335
168
  </Window>
336
-
337
169
  ```
338
170
 
339
-
340
-
341
- ```C#
171
+ ```cs
342
-
343
172
  using System;
344
-
345
173
  using System.Linq;
346
-
347
174
  using System.Windows;
348
-
349
175
  using System.Windows.Controls;
350
176
 
351
-
352
-
353
177
  namespace Questions259221
354
-
355
178
  {
356
-
357
179
  public partial class MainWindow : Window
358
-
359
180
  {
360
-
361
181
  public MainWindow()
362
-
363
182
  {
364
-
365
183
  InitializeComponent();
366
184
 
367
-
368
-
369
185
  var now = DateTime.Now;
370
-
371
186
  ChangeYEAR.ItemsSource = Enumerable.Range(now.Year - 10, 21);
372
-
373
187
  ChangeYEAR.SelectedItem = now.Year;
374
188
 
375
-
376
-
377
189
  ChangeMONTH.ItemsSource = Enumerable.Range(1, 12);
378
-
379
190
  ChangeMONTH.SelectedItem = now.Month;
380
-
381
191
  }
382
192
 
383
-
384
-
385
193
  private void SelectionChanged(object sender, SelectionChangedEventArgs e)
386
-
387
194
  {
388
-
389
195
  if(ChangeYEAR.SelectedItem is int year && ChangeMONTH.SelectedItem is int month)
390
-
391
196
  {
392
-
393
197
  var firstDate = new DateTime(year, month, 1);
394
-
395
198
  var lastDay = firstDate.AddMonths(1).AddDays(-1).Day;
396
-
397
199
  var dayOfWeek = (int)firstDate.DayOfWeek;
398
200
 
399
-
400
-
401
201
  var spacer = Enumerable.Repeat(DateTime.MinValue, dayOfWeek);
402
-
403
202
  var days = Enumerable.Range(0, lastDay).Select(x => firstDate.AddDays(x));
404
-
405
203
  var items = spacer.Concat(days);
406
-
407
204
  calendarSelector.ItemsSource = items;
408
-
409
205
  calendarView.ItemsSource = items;
410
-
411
206
  calendar.DisplayDate = firstDate;
412
-
413
207
  }
414
-
415
208
  }
416
209
 
417
-
418
-
419
210
  private void Today_Click(object sender, RoutedEventArgs e)
420
-
421
211
  {
422
-
423
212
  var now = DateTime.Now;
424
-
425
213
  ChangeYEAR.SelectedItem = now.Year;
426
-
427
214
  ChangeMONTH.SelectedItem = now.Month;
428
-
429
215
  calendarSelector.SelectedItem = now.Date;
430
-
431
216
  calendar.DisplayDate = now.Date;
432
-
433
217
  }
434
-
435
218
  }
436
-
437
219
  }
438
-
439
220
  ```
440
-
441
221
  ![イメージ説明](6e8284cbf50171078b4247541219d4f6.png)
442
222
 
443
-
444
-
445
223
  参考 [ItemsControl 攻略 ~ 外観のカスタマイズ | grabacr.nét](http://grabacr.net/archives/1240)