回答編集履歴

1

見直しキャンペーン中

2023/07/26 15:32

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,549 +1,276 @@
1
1
  気になった点の改善版
2
2
 
3
-
4
-
5
3
  文字数制限のためテンプレートは派手に削ったので、使用側でのカスタマイズ性はないです。キーボードナビゲーション回りもオミットしています^^;
6
-
7
4
  それでもxamlは元より長くなってしまいましたorz
8
5
 
9
-
10
-
11
6
  本体部分(`Grid`のところ)は非常に短くなりました。
12
-
13
7
  `MenuModel`もスッキリしたと思いますがどうでしょうか?
14
8
 
15
-
16
-
17
9
  `MenuModels`が`ObservableCollection`なので増減があると想定しましたが、固定数なら`CollectionView`を使うまでもないです。
18
10
 
19
-
20
-
21
11
  見た目を出来合いので済ませるならxamlの大部分は不要です。
22
-
23
12
  例えばこういうの↓
24
-
25
13
  [Material Design In XAML Toolkit v3.2 の紹介 - Qiita](https://qiita.com/soi/items/85138760233ca050297e#navigationrail)
26
14
 
27
15
 
28
-
29
-
30
-
31
- ```xaml
16
+ ```xml
32
-
33
17
  <Window
34
-
35
18
  x:Class="Questions329889_2.MainWindow"
36
-
37
19
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
38
-
39
20
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
40
-
41
21
  xmlns:local="clr-namespace:Questions329889_2"
42
-
43
22
  Width="23cm"
44
-
45
23
  Height="14.2cm">
46
24
 
47
-
48
-
49
25
  <Window.DataContext>
50
-
51
26
  <local:MainViewModel />
52
-
53
27
  </Window.DataContext>
54
28
 
55
-
56
-
57
29
  <Window.Resources>
58
-
59
30
  <Style x:Key="TabControlSidebarStyle" TargetType="{x:Type TabControl}">
60
-
61
31
  <Setter Property="Template">
62
-
63
32
  <Setter.Value>
64
-
65
33
  <ControlTemplate TargetType="{x:Type TabControl}">
66
-
67
34
  <DockPanel>
68
-
69
35
  <TabPanel Background="Gray" IsItemsHost="True" />
70
-
71
36
  <Border>
72
-
73
37
  <ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" />
74
-
75
38
  </Border>
76
-
77
39
  </DockPanel>
78
-
79
40
  </ControlTemplate>
80
-
81
41
  </Setter.Value>
82
-
83
42
  </Setter>
84
-
85
43
  </Style>
86
44
 
87
-
88
-
89
45
  <Style x:Key="TabItemSidebarButtonStyle" TargetType="{x:Type TabItem}">
90
-
91
46
  <Setter Property="IsSelected" Value="{Binding IsSelected}" />
92
-
93
47
  <Setter Property="Foreground" Value="White" />
94
-
95
48
  <Setter Property="Background" Value="Gray" />
96
-
97
49
  <Setter Property="Template">
98
-
99
50
  <Setter.Value>
100
-
101
51
  <ControlTemplate TargetType="{x:Type TabItem}">
102
-
103
52
  <Grid>
104
-
105
53
  <Border x:Name="mainBorder" Background="{TemplateBinding Background}">
106
-
107
54
  <Border x:Name="innerBorder" Opacity="0" />
108
-
109
55
  </Border>
110
-
111
56
  <ContentPresenter ContentSource="Header" Focusable="False" />
112
-
113
57
  </Grid>
114
-
115
58
  <ControlTemplate.Triggers>
116
-
117
59
  <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="True">
118
-
119
60
  <Setter TargetName="mainBorder" Property="Background" Value="#FFBEE6FD" />
120
-
121
61
  </DataTrigger>
122
-
123
62
  <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="True">
124
-
125
63
  <Setter Property="Foreground" Value="Black" />
126
-
127
64
  <Setter Property="Background" Value="White" />
128
-
129
65
  <Setter TargetName="innerBorder" Property="Opacity" Value="1" />
130
-
131
66
  </DataTrigger>
132
-
133
67
  </ControlTemplate.Triggers>
134
-
135
68
  </ControlTemplate>
136
-
137
69
  </Setter.Value>
138
-
139
70
  </Setter>
140
-
141
71
  </Style>
142
72
 
143
-
144
-
145
73
  <DataTemplate x:Key="SidebarButtonTemplate" DataType="{x:Type local:MenuModel}">
146
-
147
74
  <Border Padding="6,10">
148
-
149
75
  <StackPanel>
150
-
151
76
  <TextBlock
152
-
153
77
  FontFamily="Segoe MDL2 Assets"
154
-
155
78
  FontSize="26pt"
156
-
157
79
  Text="{Binding Icon}"
158
-
159
80
  TextAlignment="Center" />
160
-
161
81
  <TextBlock
162
-
163
82
  FontSize="8pt"
164
-
165
83
  Text="{Binding Title}"
166
-
167
84
  TextAlignment="Center" />
168
-
169
85
  </StackPanel>
170
-
171
86
  </Border>
172
-
173
87
  </DataTemplate>
174
88
 
175
-
176
-
177
89
  <DataTemplate x:Key="HomeContentTemplate" DataType="{x:Type local:MenuModel}">
178
-
179
90
  <ItemsControl ItemsSource="{Binding}">
180
-
181
91
  <ItemsControl.ItemTemplate>
182
-
183
92
  <DataTemplate>
184
-
185
93
  <Button
186
-
187
94
  Width="350"
188
-
189
95
  Height="160"
190
-
191
96
  Margin="5"
192
-
193
97
  Background="#efebe9"
194
-
195
98
  Command="{Binding DataContext.ClickMenu, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
196
-
197
99
  CommandParameter="{Binding}">
198
-
199
100
  <Button.Template>
200
-
201
101
  <ControlTemplate TargetType="{x:Type Button}">
202
-
203
102
  <Border
204
-
205
103
  Name="border"
206
-
207
104
  Background="{TemplateBinding Background}"
208
-
209
105
  CornerRadius="10">
210
-
211
106
  <ContentPresenter />
212
-
213
107
  </Border>
214
-
215
108
  <ControlTemplate.Triggers>
216
-
217
109
  <Trigger Property="IsMouseOver" Value="True">
218
-
219
110
  <Setter TargetName="border" Property="Background" Value="#FFBEE6FD" />
220
-
221
111
  </Trigger>
222
-
223
112
  </ControlTemplate.Triggers>
224
-
225
113
  </ControlTemplate>
226
-
227
114
  </Button.Template>
228
-
229
115
  <DockPanel Margin="10">
230
-
231
116
  <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
232
-
233
117
  <TextBlock
234
-
235
118
  Margin="10,0,10,0"
236
-
237
119
  HorizontalAlignment="Center"
238
-
239
120
  VerticalAlignment="Center"
240
-
241
121
  FontFamily="Segoe MDL2 Assets"
242
-
243
122
  FontSize="18pt"
244
-
245
123
  Text="{Binding Icon}" />
246
-
247
124
  <TextBlock FontSize="18pt" Text="{Binding Title}" />
248
-
249
125
  </StackPanel>
250
-
251
126
  <TextBlock
252
-
253
127
  FontSize="11pt"
254
-
255
128
  Text="{Binding Description}"
256
-
257
129
  TextWrapping="Wrap" />
258
-
259
130
  </DockPanel>
260
-
261
131
  </Button>
262
-
263
132
  </DataTemplate>
264
-
265
133
  </ItemsControl.ItemTemplate>
266
-
267
134
  <ItemsControl.ItemsPanel>
268
-
269
135
  <ItemsPanelTemplate>
270
-
271
136
  <WrapPanel HorizontalAlignment="Center" />
272
-
273
137
  </ItemsPanelTemplate>
274
-
275
138
  </ItemsControl.ItemsPanel>
276
-
277
139
  </ItemsControl>
278
-
279
140
  </DataTemplate>
280
141
 
281
-
282
-
283
142
  <local:HomeContentTemplateSelector x:Key="HomeContentTemplateSelector" Template="{StaticResource HomeContentTemplate}" />
284
143
 
285
-
286
-
287
144
  <DataTemplate x:Key="MenuModelContentTemplate" DataType="{x:Type local:MenuModel}">
288
-
289
145
  <ScrollViewer>
290
-
291
146
  <StackPanel>
292
-
293
147
  <TextBlock
294
-
295
148
  Margin="15"
296
-
297
149
  FontSize="23pt"
298
-
299
150
  Foreground="Gray"
300
-
301
151
  Text="{Binding Title}" />
302
-
303
152
  <ContentPresenter
304
-
305
153
  Margin="15"
306
-
307
154
  Content="{Binding Content}"
308
-
309
155
  ContentTemplateSelector="{StaticResource HomeContentTemplateSelector}" />
310
-
311
156
  </StackPanel>
312
-
313
157
  </ScrollViewer>
314
-
315
158
  </DataTemplate>
316
-
317
159
  </Window.Resources>
318
160
 
319
-
320
-
321
161
  <Grid>
322
-
323
162
  <TabControl
324
-
325
163
  ContentTemplate="{StaticResource MenuModelContentTemplate}"
326
-
327
164
  ItemContainerStyle="{StaticResource TabItemSidebarButtonStyle}"
328
-
329
165
  ItemTemplate="{StaticResource SidebarButtonTemplate}"
330
-
331
166
  ItemsSource="{Binding MenuModels}"
332
-
333
167
  Style="{StaticResource TabControlSidebarStyle}"
334
-
335
168
  TabStripPlacement="Left" />
336
169
 
337
-
338
-
339
170
  <Button
340
-
341
171
  Margin="15"
342
-
343
172
  HorizontalAlignment="Right"
344
-
345
173
  VerticalAlignment="Top"
346
-
347
174
  Command="{Binding AddCommand}"
348
-
349
175
  Content="Add" />
350
-
351
176
  </Grid>
352
-
353
177
  </Window>
354
-
355
178
  ```
356
179
 
357
-
358
-
359
- ```C#
180
+ ```cs
360
-
361
181
  using Prism.Commands;
362
-
363
182
  using Prism.Mvvm;
364
-
365
183
  using System.Collections.ObjectModel;
366
-
367
184
  using System.Windows;
368
-
369
185
  using System.Windows.Controls;
370
-
371
186
  using System.Windows.Data;
372
187
 
373
-
374
-
375
188
  namespace Questions329889_2
376
-
377
189
  {
378
-
379
190
  public class HomeContentTemplateSelector : DataTemplateSelector
380
-
381
- {
191
+ {
382
-
383
192
  public override DataTemplate SelectTemplate(object item, DependencyObject container)
384
-
385
193
  => item is CollectionView ? Template : null;
386
-
387
194
  public DataTemplate Template { get; set; }
388
-
389
- }
195
+ }
390
-
391
-
392
196
 
393
197
  public class MenuModel : BindableBase
394
-
395
- {
198
+ {
396
-
397
199
  public string Icon { get; init; }
398
-
399
200
  public string Title { get; init; }
400
-
401
201
  public string Description { get; init; }
402
-
403
202
  public object Content { get; set; }
404
203
 
405
-
406
-
407
204
  public bool IsSelected { get => _IsSelected; set => SetProperty(ref _IsSelected, value); }
408
-
409
205
  private bool _IsSelected;
410
-
411
- }
206
+ }
412
-
413
-
414
207
 
415
208
  public class MainViewModel : BindableBase
416
-
417
- {
209
+ {
418
-
419
210
  public ObservableCollection<MenuModel> MenuModels { get; }
420
-
421
211
  public DelegateCommand<MenuModel> ClickMenu { get; }
422
-
423
212
  public DelegateCommand AddCommand { get; }
424
213
 
425
-
426
-
427
214
  public MainViewModel()
428
-
429
215
  {
430
-
431
216
  MenuModels = new()
432
-
433
- {
217
+ {
434
-
435
- new()
218
+ new()
436
-
437
- {
219
+ {
438
-
439
220
  IsSelected = true,
440
-
441
221
  Icon = "\xE10F",
442
-
443
222
  Title = "HOME",
444
-
445
- },
223
+ },
446
-
447
- new()
224
+ new()
448
-
449
- {
225
+ {
450
-
451
226
  Icon = "\xF157",
452
-
453
227
  Title = "CONTENT1",
454
-
455
228
  Description = "CONTENT1 です。ここには、CONTENT1 の説明が入ります。",
456
-
457
229
  Content = "CONTENT1 です。",
458
-
459
- },
230
+ },
460
-
461
- new()
231
+ new()
462
-
463
- {
232
+ {
464
-
465
233
  Icon = "\xF158",
466
-
467
234
  Title = "CONTENT2",
468
-
469
235
  Description = "CONTENT2 です。ここには、CONTENT2 の説明が入ります。",
470
-
471
- },
236
+ },
472
-
473
- new()
237
+ new()
474
-
475
- {
238
+ {
476
-
477
239
  Icon = "\xF159",
478
-
479
240
  Title = "CONTENT3",
480
-
481
241
  Description = "CONTENT3 です。ここには、CONTENT3 の説明が入ります。",
482
-
483
- },
242
+ },
484
-
485
243
  };
486
244
 
487
-
488
-
489
245
  MenuModels[0].Content = new ListCollectionView(MenuModels)
490
-
491
- {
246
+ {
492
-
493
247
  Filter = (x) => ((MenuModel)x).Title != "HOME",
494
-
495
248
  };
496
249
 
497
-
498
-
499
250
  ClickMenu = new(x =>
500
-
501
- {
251
+ {
502
-
503
252
  foreach (var m in MenuModels)
504
-
505
253
  m.IsSelected = x == m;
506
-
507
254
  });
508
255
 
509
-
510
-
511
256
  AddCommand = new(() =>
512
-
513
- {
257
+ {
514
-
515
258
  MenuModels.Add(new()
516
-
517
- {
259
+ {
518
-
519
260
  Icon = "\xF159",
520
-
521
261
  Title = $"CONTENT{number}",
522
-
523
262
  Description = $"CONTENT{number} です。ここには、CONTENT{number} の説明が入ります。",
524
-
525
263
  });
526
-
527
264
  number++;
528
-
529
265
  });
530
-
531
266
  }
532
-
533
267
  private int number = 4;
534
-
535
- }
268
+ }
536
-
537
-
538
269
 
539
270
  public partial class MainWindow : Window
540
-
541
- {
271
+ {
542
-
543
272
  public MainWindow() => InitializeComponent();
544
-
545
- }
273
+ }
546
-
547
274
  }
548
-
549
275
  ```
276
+ ![アプリ画像](https://ddjkaamml8q8x.cloudfront.net/questions/2023-07-27/a72b596e-600f-4e74-bc9a-35db8125ca58.png)