回答編集履歴

1

見直しキャンペーン中

2023/07/26 15:21

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,527 +1,264 @@
1
1
  > あるコレクションのメソッドから他のコレクションの値を変更する事ってできないですよね?
2
2
 
3
-
4
-
5
3
  `ChangeParameter`のところの話だと思いますが、表示・非表示を引数に全員回せばいいでしょう。
6
-
7
- ```C#
4
+ ```cs
8
-
9
5
  ClickMenu = new(x =>
10
-
11
6
  {
12
-
13
7
  foreach (var m in MenuModels)
14
-
15
8
  m.ChangeParameter(x == m);
16
-
17
9
  });
18
-
19
10
  ```
20
11
 
21
-
22
-
23
12
  ---
24
13
 
25
-
26
-
27
14
  とりあえず提示コードを動かすなら、こんな感じでしょうか?
28
-
29
- ```xaml
15
+ ```xml
30
-
31
16
  <Window
32
-
33
17
  x:Class="Questions329889.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
34
-
35
18
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:Questions329889" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
36
-
37
19
  Title="MainWindow" Width="23cm" Height="14.2cm"
38
-
39
20
  MinWidth="13cm" MinHeight="14.2cm" mc:Ignorable="d">
40
-
41
21
  <Window.DataContext>
42
-
43
22
  <local:MainViewModel />
44
-
45
23
  </Window.DataContext>
46
-
47
24
  <Window.Resources>
48
-
49
25
  <local:String2BrushConverter x:Key="String2BrushConverter" />
50
-
51
26
  </Window.Resources>
52
-
53
27
  <Grid>
54
-
55
28
  <Grid.ColumnDefinitions>
56
-
57
29
  <ColumnDefinition Width="2cm" />
58
-
59
30
  <ColumnDefinition MinWidth="21" />
60
-
61
31
  </Grid.ColumnDefinitions>
62
32
 
63
-
64
-
65
33
  <Grid Grid.Column="1" Visibility="{Binding MenuModels[0].Visibility}">
66
-
67
- <ScrollViewer>
34
+ <ScrollViewer>
68
-
69
- <StackPanel>
35
+ <StackPanel>
70
-
71
- <TextBlock
36
+ <TextBlock
72
-
73
- Margin="15,15,0,0" FontSize="23pt" Foreground="#808080"
37
+ Margin="15,15,0,0" FontSize="23pt" Foreground="#808080"
74
-
75
38
  Text="{Binding MenuModels[0].Content}" />
76
39
 
77
-
78
-
79
40
  <ItemsControl x:Name="MyItems" ItemsSource="{Binding MenuModels}">
80
-
81
41
  <ItemsControl.ItemTemplate>
82
-
83
42
  <DataTemplate>
84
-
85
43
  <Grid Margin="15,15,0,0" Visibility="{Binding HomeMenuVisibility}">
86
-
87
44
  <Border Background="#efebe9" CornerRadius="10" />
88
-
89
45
  <Button
90
-
91
46
  Width="350" Height="160" Background="{x:Null}"
92
-
93
47
  BorderThickness="0" Command="{Binding ElementName=MyItems, Path=DataContext.ClickMenu}" CommandParameter="{Binding}">
94
-
95
48
  <StackPanel
96
-
97
49
  Width="330" Height="140" Margin="10">
98
-
99
50
  <StackPanel Orientation="Horizontal">
100
-
101
51
  <TextBlock
102
-
103
52
  Margin="10,0,10,0" HorizontalAlignment="Center" VerticalAlignment="Center"
104
-
105
53
  FontFamily="Segoe MDL2 Assets" FontSize="18pt" Text="{Binding Icon}" />
106
-
107
54
  <TextBlock FontSize="18pt" Text="{Binding Content}" />
108
-
109
55
  </StackPanel>
110
-
111
56
  <TextBlock
112
-
113
57
  FontSize="11pt" Text="{Binding Note}" TextWrapping="Wrap" />
114
-
115
58
  </StackPanel>
116
-
117
59
  </Button>
118
-
119
60
  </Grid>
120
-
121
61
  </DataTemplate>
122
-
123
62
  </ItemsControl.ItemTemplate>
124
-
125
63
  <ItemsControl.ItemsPanel>
126
-
127
64
  <ItemsPanelTemplate>
128
-
129
65
  <WrapPanel HorizontalAlignment="Center" />
130
-
131
66
  </ItemsPanelTemplate>
132
-
133
67
  </ItemsControl.ItemsPanel>
134
-
135
68
  </ItemsControl>
136
-
137
- </StackPanel>
69
+ </StackPanel>
138
-
139
- </ScrollViewer>
70
+ </ScrollViewer>
140
-
141
- </Grid>
71
+ </Grid>
142
-
143
-
144
72
 
145
73
  <Grid Grid.Column="1" Visibility="{Binding MenuModels[1].Visibility}">
146
-
147
- <ScrollViewer>
74
+ <ScrollViewer>
148
-
149
- <StackPanel>
75
+ <StackPanel>
150
-
151
- <TextBlock
76
+ <TextBlock
152
-
153
- Margin="15,15,0,0" FontSize="23pt" Foreground="#808080"
77
+ Margin="15,15,0,0" FontSize="23pt" Foreground="#808080"
154
-
155
78
  Text="{Binding MenuModels[1].Content}" />
156
-
157
- </StackPanel>
79
+ </StackPanel>
158
-
159
- </ScrollViewer>
80
+ </ScrollViewer>
160
-
161
- </Grid>
81
+ </Grid>
162
-
163
-
164
82
 
165
83
  <Grid Grid.Column="1" Visibility="{Binding MenuModels[2].Visibility}">
166
-
167
- <ScrollViewer>
84
+ <ScrollViewer>
168
-
169
- <StackPanel>
85
+ <StackPanel>
170
-
171
- <TextBlock
86
+ <TextBlock
172
-
173
- Margin="15,15,0,0" FontSize="23pt" Foreground="#808080"
87
+ Margin="15,15,0,0" FontSize="23pt" Foreground="#808080"
174
-
175
88
  Text="{Binding MenuModels[2].Content}" />
176
-
177
- </StackPanel>
89
+ </StackPanel>
178
-
179
- </ScrollViewer>
90
+ </ScrollViewer>
180
-
181
- </Grid>
91
+ </Grid>
182
-
183
-
184
92
 
185
93
  <Grid Grid.Column="1" Visibility="{Binding MenuModels[3].Visibility}">
186
-
187
- <ScrollViewer>
94
+ <ScrollViewer>
188
-
189
- <StackPanel>
95
+ <StackPanel>
190
-
191
- <TextBlock
96
+ <TextBlock
192
-
193
- Margin="15,15,0,0" FontSize="23pt" Foreground="#808080"
97
+ Margin="15,15,0,0" FontSize="23pt" Foreground="#808080"
194
-
195
98
  Text="{Binding MenuModels[3].Content}" />
196
-
197
- </StackPanel>
99
+ </StackPanel>
198
-
199
- </ScrollViewer>
100
+ </ScrollViewer>
200
-
201
- </Grid>
101
+ </Grid>
202
-
203
-
204
102
 
205
103
  <Grid Background="#808080">
206
-
207
104
  <ItemsControl x:Name="MyItemsMenu" ItemsSource="{Binding MenuModels}">
208
-
209
105
  <ItemsControl.ItemTemplate>
210
-
211
106
  <DataTemplate>
212
-
213
107
  <Button
214
-
215
108
  Padding="10" Background="{Binding SideMenuBackground, Converter={StaticResource String2BrushConverter}}" BorderThickness="0"
216
-
217
109
  Command="{Binding DataContext.ClickMenu, ElementName=MyItemsMenu}" CommandParameter="{Binding}" Foreground="{Binding SideMenuForeground, Converter={StaticResource String2BrushConverter}}">
218
-
219
110
  <StackPanel>
220
-
221
111
  <TextBlock
222
-
223
112
  FontFamily="Segoe MDL2 Assets" FontSize="26pt" Text="{Binding Icon}"
224
-
225
113
  TextAlignment="Center" />
226
-
227
114
  <TextBlock
228
-
229
115
  FontSize="8pt" Text="{Binding Content}" TextAlignment="Center" />
230
-
231
116
  </StackPanel>
232
-
233
117
  </Button>
234
-
235
118
  </DataTemplate>
236
-
237
119
  </ItemsControl.ItemTemplate>
238
-
239
120
  <ItemsControl.ItemsPanel>
240
-
241
121
  <ItemsPanelTemplate>
242
-
243
122
  <StackPanel />
244
-
245
123
  </ItemsPanelTemplate>
246
-
247
124
  </ItemsControl.ItemsPanel>
248
-
249
125
  </ItemsControl>
250
-
251
- </Grid>
126
+ </Grid>
252
-
253
127
  </Grid>
254
-
255
128
  </Window>
256
-
257
129
  ```
258
130
 
259
-
260
-
261
- ```C#
131
+ ```cs
262
-
263
132
  using Prism.Commands;
264
-
265
133
  using Prism.Mvvm;
266
-
267
134
  using System;
268
-
269
135
  using System.Collections.ObjectModel;
270
-
271
136
  using System.Diagnostics;
272
-
273
137
  using System.Globalization;
274
-
275
138
  using System.Windows;
276
-
277
139
  using System.Windows.Data;
278
-
279
140
  using System.Windows.Media;
280
141
 
281
-
282
-
283
142
  namespace Questions329889
284
-
285
143
  {
286
-
287
144
  public class String2BrushConverter : IValueConverter
288
-
289
- {
145
+ {
290
-
291
146
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
292
-
293
147
  {
294
-
295
148
  if (value is string s)
296
-
297
149
  return new SolidColorBrush((Color)ColorConverter.ConvertFromString(s));
298
-
299
150
  return null;
300
-
301
151
  }
302
-
303
152
  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException();
304
-
305
- }
153
+ }
306
-
307
-
308
154
 
309
155
  public class MenuModel : BindableBase
310
-
311
- {
156
+ {
312
-
313
157
  public string SideMenuForeground { get => _SideMenuForeground; set => SetProperty(ref _SideMenuForeground, value); }
314
-
315
158
  private string _SideMenuForeground;
316
159
 
317
-
318
-
319
160
  public string SideMenuBackground { get => _SideMenuBackground; set => SetProperty(ref _SideMenuBackground, value); }
320
-
321
161
  private string _SideMenuBackground;
322
162
 
323
-
324
-
325
163
  public Visibility HomeMenuVisibility { get => _HomeMenuVisibility; set => SetProperty(ref _HomeMenuVisibility, value); }
326
-
327
164
  private Visibility _HomeMenuVisibility;
328
165
 
329
-
330
-
331
166
  public Visibility SideMenuVisibility { get => _SideMenuVisibility; set => SetProperty(ref _SideMenuVisibility, value); }
332
-
333
167
  private Visibility _SideMenuVisibility;
334
168
 
335
-
336
-
337
169
  public Visibility Visibility { get => _Visibility; set => SetProperty(ref _Visibility, value); }
338
-
339
170
  private Visibility _Visibility;
340
171
 
341
-
342
-
343
172
  public string Icon { get => _Icon; set => SetProperty(ref _Icon, value); }
344
-
345
173
  private string _Icon;
346
174
 
347
-
348
-
349
175
  public string Content { get => _Content; set => SetProperty(ref _Content, value); }
350
-
351
176
  private string _Content;
352
177
 
353
-
354
-
355
178
  public string Note { get => _Note; set => SetProperty(ref _Note, value); }
356
-
357
179
  private string _Note;
358
180
 
359
-
360
-
361
181
  public void ChangeParameter(bool visible)
362
-
363
182
  {
364
-
365
183
  if (visible) Debug.WriteLine($"Visible: {Content}");
366
-
367
184
  Visibility = visible ? Visibility.Visible : Visibility.Hidden;
368
-
369
185
  SideMenuBackground = visible ? "#fff" : null;
370
-
371
186
  SideMenuForeground = visible ? "#000" : "#fff";
372
-
373
187
  }
374
-
375
- }
188
+ }
376
-
377
-
378
189
 
379
190
  public class MainViewModel : BindableBase
380
-
381
- {
191
+ {
382
-
383
192
  public ObservableCollection<MenuModel> MenuModels { get; }
384
-
385
193
  public DelegateCommand<MenuModel> ClickMenu { get; }
386
194
 
387
-
388
-
389
195
  public MainViewModel()
390
-
391
196
  {
392
-
393
197
  MenuModels = new()
394
-
395
198
  {
396
-
397
- new()
199
+ new()
398
-
399
- {
200
+ {
400
-
401
201
  SideMenuBackground = "#fff",
402
-
403
202
  SideMenuForeground = "#000",
404
-
405
203
  HomeMenuVisibility = Visibility.Collapsed,
406
-
407
- SideMenuVisibility = Visibility.Visible,
204
+ SideMenuVisibility = Visibility.Visible,
408
-
409
205
  Visibility = Visibility.Visible,
410
-
411
206
  Icon = "\xE10F",
412
-
413
207
  Content = "HOME",
414
-
415
- },
208
+ },
416
-
417
- new()
209
+ new()
418
-
419
- {
210
+ {
420
-
421
211
  SideMenuForeground = "#fff",
422
-
423
212
  HomeMenuVisibility = Visibility.Visible,
424
-
425
- SideMenuVisibility = Visibility.Visible,
213
+ SideMenuVisibility = Visibility.Visible,
426
-
427
214
  Visibility = Visibility.Hidden,
428
-
429
215
  Icon = "\xF157",
430
-
431
216
  Content = "CONTENT1",
432
-
433
217
  Note = "Content1 です。ここには、content1 の説明が入ります。",
434
-
435
- },
218
+ },
436
-
437
- new()
219
+ new()
438
-
439
- {
220
+ {
440
-
441
221
  SideMenuForeground = "#fff",
442
-
443
222
  HomeMenuVisibility = Visibility.Visible,
444
-
445
- SideMenuVisibility = Visibility.Visible,
223
+ SideMenuVisibility = Visibility.Visible,
446
-
447
224
  Visibility = Visibility.Hidden,
448
-
449
225
  Icon = "\xF158",
450
-
451
226
  Content = "CONTENT2",
452
-
453
227
  Note = "Content2 です。ここには、content2 の説明が入ります。",
454
-
455
- },
228
+ },
456
-
457
- new()
229
+ new()
458
-
459
- {
230
+ {
460
-
461
231
  SideMenuForeground = "#fff",
462
-
463
232
  HomeMenuVisibility = Visibility.Visible,
464
-
465
- SideMenuVisibility = Visibility.Visible,
233
+ SideMenuVisibility = Visibility.Visible,
466
-
467
234
  Visibility = Visibility.Hidden,
468
-
469
235
  Icon = "\xF159",
470
-
471
236
  Content = "CONTENT3",
472
-
473
237
  Note = "Content3 です。ここには、content3 の説明が入ります。",
474
-
475
- },
238
+ },
476
-
477
239
  };
478
240
 
479
-
480
-
481
241
  ClickMenu = new(x =>
482
-
483
242
  {
484
-
485
243
  foreach (var m in MenuModels)
486
-
487
244
  m.ChangeParameter(x == m);
488
-
489
245
  });
490
-
491
246
  }
492
-
493
- }
247
+ }
494
-
495
-
496
248
 
497
249
  public partial class MainWindow : Window
498
-
499
- {
250
+ {
500
-
501
251
  public MainWindow() => InitializeComponent();
502
-
503
- }
252
+ }
504
-
505
253
  }
506
-
507
254
  ```
508
255
 
509
-
510
-
511
256
  ---
512
257
 
513
-
514
-
515
258
  気になった点
516
-
517
259
  * ボタンの扱いがおかしい(文字等がある部分までマウスを入れないと反応しない)
518
-
519
260
  見た目を変えるにはテンプレートを当ててください。
520
-
521
261
  * `View`だけの関心が`ViewModel`(`MenuModel`)に染み出している
522
-
523
262
  `MenuModel`では選択されているという情報だけでいいと思います。
524
-
525
263
  * おそらく`TabControl`ですっきり書けそう
526
-
527
264
  見た目を同じにするとなると、スタイル等で行数は変わらないかも?