回答編集履歴

1

見直しキャンペーン中

2023/07/22 09:00

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,363 +1,182 @@
1
1
  多段のカラムヘッダーがやりたいと仮定してちょっと考えてみました。
2
2
 
3
-
4
-
5
3
  * カラムサイズ変更・カラム入れ替えを禁止。
6
-
7
4
  * サイズは比率指定のみ(`DataGrid`自体のサイズは変わるが横スクロールバーが出ない状態)
8
-
9
5
  * 横スクロールバー常に非表示・縦スクロールバー常に表示
10
-
11
-
12
6
 
13
7
  という限定された条件なら、ヘッダーのみのダミー`DataGrid`を適当に積み重ねれば手軽に実現できますね(非常にバカバカしいですが^^;
14
8
 
15
-
16
-
17
- ```xaml
9
+ ```xml
18
-
19
10
  <Window
20
-
21
11
  x:Class="Questions276512.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
  xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"
28
-
29
15
  xmlns:sys="clr-namespace:System;assembly=mscorlib"
30
-
31
16
  Width="800"
32
-
33
17
  Height="400">
34
-
35
18
  <Window.Resources>
36
-
37
19
  <!-- 特に意味はない xamlのみ縛り -->
38
-
39
20
  <collections:ArrayList x:Key="data">
40
-
41
21
  <collections:DictionaryEntry Key="仙台支店">
42
-
43
22
  <collections:DictionaryEntry.Value>
44
-
45
23
  <collections:ArrayList>
46
-
47
24
  <sys:Int32>1</sys:Int32>
48
-
49
25
  <sys:Int32>2</sys:Int32>
50
-
51
26
  <sys:Int32>3</sys:Int32>
52
-
53
27
  </collections:ArrayList>
54
-
55
28
  </collections:DictionaryEntry.Value>
56
-
57
29
  </collections:DictionaryEntry>
58
-
59
30
  <collections:DictionaryEntry Key="東京本店">
60
-
61
31
  <collections:DictionaryEntry.Value>
62
-
63
32
  <collections:ArrayList>
64
-
65
33
  <sys:Int32>10</sys:Int32>
66
-
67
34
  <sys:Int32>20</sys:Int32>
68
-
69
35
  <sys:Int32>30</sys:Int32>
70
-
71
36
  </collections:ArrayList>
72
-
73
37
  </collections:DictionaryEntry.Value>
74
-
75
38
  </collections:DictionaryEntry>
76
-
77
39
  </collections:ArrayList>
78
40
 
79
-
80
-
81
41
  <Style TargetType="DataGridColumnHeader">
82
-
83
42
  <Setter Property="HorizontalContentAlignment" Value="Center" />
84
-
85
43
  </Style>
86
-
87
44
  </Window.Resources>
88
45
 
89
-
90
-
91
46
  <Border
92
-
93
47
  Margin="10"
94
-
95
48
  BorderBrush="#FF688CAF"
96
-
97
49
  BorderThickness="1">
98
-
99
50
  <Grid>
100
-
101
51
  <Grid.RowDefinitions>
102
-
103
52
  <RowDefinition Height="Auto" />
104
-
105
53
  <RowDefinition Height="Auto" />
106
-
107
54
  <RowDefinition />
108
-
109
55
  </Grid.RowDefinitions>
110
56
 
111
-
112
-
113
57
  <!-- 偽ヘッダー1 -->
114
-
115
58
  <Grid Background="#FFF0F0F0">
116
-
117
59
  <Grid.ColumnDefinitions>
118
-
119
60
  <ColumnDefinition Width="Auto" />
120
-
121
61
  <ColumnDefinition />
122
-
123
62
  <ColumnDefinition Width="Auto" />
124
-
125
63
  </Grid.ColumnDefinitions>
126
-
127
64
  <Border Width="{Binding RowHeaderActualWidth, ElementName=datagrid1, Mode=OneWay}" Visibility="{Binding VerticalScrollBarVisibility, ElementName=datagrid1}" />
128
-
129
65
  <DataGrid
130
-
131
66
  Grid.Column="1"
132
-
133
67
  BorderThickness="0"
134
-
135
68
  CanUserReorderColumns="False"
136
-
137
69
  CanUserResizeColumns="False"
138
-
139
70
  CanUserSortColumns="False">
140
-
141
71
  <DataGrid.Columns>
142
-
143
72
  <DataGridTextColumn Width="*" Header="2020年度" />
144
-
145
73
  </DataGrid.Columns>
146
-
147
74
  </DataGrid>
148
-
149
75
  <ScrollBar Grid.Column="2" Visibility="Hidden" />
150
-
151
76
  </Grid>
152
77
 
153
-
154
-
155
78
  <!-- 偽ヘッダー2 -->
156
-
157
79
  <Grid Grid.Row="1" Background="#FFF0F0F0">
158
-
159
80
  <Grid.ColumnDefinitions>
160
-
161
81
  <ColumnDefinition Width="Auto" />
162
-
163
82
  <ColumnDefinition />
164
-
165
83
  <ColumnDefinition Width="Auto" />
166
-
167
84
  </Grid.ColumnDefinitions>
168
-
169
85
  <Border Width="{Binding RowHeaderActualWidth, ElementName=datagrid1, Mode=OneWay}" />
170
-
171
86
  <DataGrid
172
-
173
87
  Grid.Column="1"
174
-
175
88
  BorderThickness="0"
176
-
177
89
  CanUserReorderColumns="False"
178
-
179
90
  CanUserResizeColumns="False"
180
-
181
91
  CanUserSortColumns="False">
182
-
183
92
  <DataGrid.Columns>
184
-
185
93
  <DataGridTextColumn Width="*" Header="第一四半期" />
186
-
187
94
  <DataGridTextColumn Width="*" Header="第二四半期" />
188
-
189
95
  <DataGridTextColumn Width="*" Header="第三四半期" />
190
-
191
96
  <DataGridTextColumn Width="*" Header="第四四半期" />
192
-
193
97
  </DataGrid.Columns>
194
-
195
98
  </DataGrid>
196
-
197
99
  <ScrollBar Grid.Column="2" Visibility="Hidden" />
198
-
199
100
  </Grid>
200
101
 
201
-
202
-
203
102
  <!-- 本体 -->
204
-
205
103
  <DataGrid
206
-
207
104
  Name="datagrid1"
208
-
209
105
  Grid.Row="2"
210
-
211
106
  AutoGenerateColumns="False"
212
-
213
107
  BorderThickness="0"
214
-
215
108
  CanUserReorderColumns="False"
216
-
217
109
  CanUserResizeColumns="False"
218
-
219
110
  HeadersVisibility="All"
220
-
221
111
  HorizontalScrollBarVisibility="Disabled"
222
-
223
112
  ItemsSource="{StaticResource data}"
224
-
225
113
  VerticalScrollBarVisibility="Visible">
226
-
227
114
  <DataGrid.RowHeaderStyle>
228
-
229
115
  <Style TargetType="{x:Type DataGridRowHeader}">
230
-
231
116
  <Setter Property="Content" Value="{Binding Key}" />
232
-
233
117
  </Style>
234
-
235
118
  </DataGrid.RowHeaderStyle>
236
-
237
119
  <DataGrid.Columns>
238
-
239
120
  <DataGridTextColumn
240
-
241
121
  Width="*"
242
-
243
122
  Binding="{Binding Value[0]}"
244
-
245
123
  Header="4月" />
246
-
247
124
  <DataGridTextColumn
248
-
249
125
  Width="*"
250
-
251
126
  Binding="{Binding Value[1]}"
252
-
253
127
  Header="5月" />
254
-
255
128
  <DataGridTextColumn
256
-
257
129
  Width="*"
258
-
259
130
  Binding="{Binding Value[2]}"
260
-
261
131
  Header="6月" />
262
-
263
132
  <DataGridTextColumn
264
-
265
133
  Width="*"
266
-
267
134
  Binding="{Binding Value[3]}"
268
-
269
135
  Header="7月" />
270
-
271
136
  <DataGridTextColumn
272
-
273
137
  Width="*"
274
-
275
138
  Binding="{Binding Value[4]}"
276
-
277
139
  Header="8月" />
278
-
279
140
  <DataGridTextColumn
280
-
281
141
  Width="*"
282
-
283
142
  Binding="{Binding Value[5]}"
284
-
285
143
  Header="9月" />
286
-
287
144
  <DataGridTextColumn
288
-
289
145
  Width="*"
290
-
291
146
  Binding="{Binding Value[6]}"
292
-
293
147
  Header="10月" />
294
-
295
148
  <DataGridTextColumn
296
-
297
149
  Width="*"
298
-
299
150
  Binding="{Binding Value[7]}"
300
-
301
151
  Header="11月" />
302
-
303
152
  <DataGridTextColumn
304
-
305
153
  Width="*"
306
-
307
154
  Binding="{Binding Value[8]}"
308
-
309
155
  Header="12月" />
310
-
311
156
  <DataGridTextColumn
312
-
313
157
  Width="*"
314
-
315
158
  Binding="{Binding Value[9]}"
316
-
317
159
  Header="1月" />
318
-
319
160
  <DataGridTextColumn
320
-
321
161
  Width="*"
322
-
323
162
  Binding="{Binding Value[10]}"
324
-
325
163
  Header="2月" />
326
-
327
164
  <DataGridTextColumn
328
-
329
165
  Width="*"
330
-
331
166
  Binding="{Binding Value[11]}"
332
-
333
167
  Header="3月" />
334
-
335
168
  </DataGrid.Columns>
336
-
337
169
  </DataGrid>
338
-
339
170
  </Grid>
340
-
341
171
  </Border>
342
-
343
172
  </Window>
344
-
345
173
  ```
346
-
347
174
  無駄にxamlのみ縛りしたのと見た目にこだわったので、少し長くなりましたがやってることは単純です。
348
-
349
175
  ![アプリ画像](5efd977c949a7ce7a888a70ef1b80e66.png)
350
-
351
-
352
176
 
353
177
  ---
354
178
 
355
-
356
-
357
179
  しかし個人的にはこれでは使う気になれないです^^;
358
-
359
180
  なんとかカラムサイズ変更だけは可能にしたいと思い、頑張っていたのですが`IMultiValueConverter`だけは書く必要がありますねぇ(上下のサイズ変更が追従する。ここまでは検証済み)
360
181
 
361
-
362
-
363
182
  横スクロール連動となるとビヘイビアにするよりなさそうですし、この先は未検証です。