回答編集履歴

1

見直しキャンペーン中

2023/07/28 16:40

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,537 +1,269 @@
1
1
  追記では収まらないので、別回答にさせていただきます。
2
2
 
3
-
4
-
5
3
  やっていることは前の回答と同じなのですが、より具体的かつシンプルになっています。
6
-
7
4
  そのためまったく汎用的ではありません。
8
5
 
9
-
10
-
11
- ```xaml
6
+ ```xml:MainWindow.xaml
12
-
13
7
  <Window x:Class="Questions355107.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Questions355107" Width="{Binding Settings.Window.Width, Mode=TwoWay}"
14
-
15
8
  Height="{Binding Settings.Window.Height, Mode=TwoWay}" Left="{Binding Settings.Window.Left, Mode=TwoWay}" Top="{Binding Settings.Window.Top, Mode=TwoWay}">
16
-
17
9
  <Window.DataContext>
18
-
19
10
  <local:ViewModel />
20
-
21
11
  </Window.DataContext>
22
-
23
12
  <Window.Resources>
24
-
25
13
  <DataTemplate DataType="{x:Type local:ContentModel}">
26
-
27
14
  <Border x:Name="b" Background="{Binding Brush}" SizeChanged="Border_SizeChanged">
28
-
29
15
  <Grid>
30
-
31
16
  <TextBlock>
32
-
33
17
  <TextBlock.Text>
34
-
35
18
  <MultiBinding StringFormat="{}{0:f2}, {1:f2}">
36
-
37
19
  <Binding ElementName="b" Path="ActualWidth" />
38
-
39
20
  <Binding ElementName="b" Path="ActualHeight" />
40
-
41
21
  </MultiBinding>
42
-
43
22
  </TextBlock.Text>
44
-
45
23
  </TextBlock>
46
-
47
24
  <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30" FontWeight="Bold" Text="{Binding Text}" />
48
-
49
25
  </Grid>
50
-
51
26
  </Border>
52
-
53
- </DataTemplate>
27
+ </DataTemplate>
54
-
55
-
56
28
 
57
29
  <DataTemplate x:Key="OnePane">
58
-
59
30
  <ContentControl Content="{Binding Contents[0]}" />
60
-
61
- </DataTemplate>
31
+ </DataTemplate>
62
-
63
32
  <DataTemplate x:Key="TwoPane">
64
-
65
33
  <local:TwoPane />
66
-
67
- </DataTemplate>
34
+ </DataTemplate>
68
-
69
35
  <DataTemplate x:Key="ThreePane">
70
-
71
36
  <local:ThreePane />
72
-
73
- </DataTemplate>
37
+ </DataTemplate>
74
-
75
38
  <DataTemplate x:Key="FourPane">
76
-
77
39
  <local:FourPane />
78
-
79
- </DataTemplate>
40
+ </DataTemplate>
80
-
81
-
82
41
 
83
42
  <Style TargetType="GridSplitter">
84
-
85
43
  <Setter Property="HorizontalAlignment" Value="Stretch" />
86
-
87
44
  </Style>
88
45
 
89
-
90
-
91
46
  <Style x:Key="PaneStyle" TargetType="{x:Type ContentControl}">
92
-
93
47
  <Style.Triggers>
94
-
95
48
  <DataTrigger Binding="{Binding SelPane}" Value="1">
96
-
97
49
  <Setter Property="ContentTemplate" Value="{StaticResource OnePane}" />
98
-
99
- </DataTrigger>
50
+ </DataTrigger>
100
-
101
51
  <DataTrigger Binding="{Binding SelPane}" Value="2">
102
-
103
52
  <Setter Property="ContentTemplate" Value="{StaticResource TwoPane}" />
104
-
105
- </DataTrigger>
53
+ </DataTrigger>
106
-
107
54
  <DataTrigger Binding="{Binding SelPane}" Value="3">
108
-
109
55
  <Setter Property="ContentTemplate" Value="{StaticResource ThreePane}" />
110
-
111
- </DataTrigger>
56
+ </DataTrigger>
112
-
113
57
  <DataTrigger Binding="{Binding SelPane}" Value="4">
114
-
115
58
  <Setter Property="ContentTemplate" Value="{StaticResource FourPane}" />
116
-
117
- </DataTrigger>
59
+ </DataTrigger>
118
-
119
60
  </Style.Triggers>
120
-
121
61
  </Style>
122
62
 
123
-
124
-
125
63
  <Style x:Key="RadioListBoxItem" TargetType="{x:Type ListBoxItem}">
126
-
127
64
  <Setter Property="Template">
128
-
129
65
  <Setter.Value>
130
-
131
66
  <ControlTemplate TargetType="{x:Type ListBoxItem}">
132
-
133
67
  <RadioButton MinWidth="30" Content="{TemplateBinding Content}" IsChecked="{Binding IsSelected, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource {x:Type ToggleButton}}" />
134
-
135
68
  </ControlTemplate>
136
-
137
69
  </Setter.Value>
138
-
139
70
  </Setter>
140
-
141
71
  </Style>
142
-
143
72
  </Window.Resources>
144
-
145
73
  <DockPanel>
146
-
147
74
  <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
148
-
149
75
  <Button Command="{Binding Save}" Content="Save" />
150
-
151
76
  <Button Command="{Binding Load}" Content="Load" />
152
-
153
77
  <ListBox Padding="-1" BorderThickness="0" ItemContainerStyle="{StaticResource RadioListBoxItem}" ItemsSource="{Binding Panes}" SelectedItem="{Binding SelPane}">
154
-
155
78
  <ListBox.ItemsPanel>
156
-
157
79
  <ItemsPanelTemplate>
158
-
159
80
  <StackPanel Orientation="Horizontal" />
160
-
161
81
  </ItemsPanelTemplate>
162
-
163
82
  </ListBox.ItemsPanel>
164
-
165
83
  </ListBox>
166
-
167
84
  <Button Command="{Binding View}" Content="View" />
168
-
169
85
  </StackPanel>
170
-
171
86
  <ContentControl Content="{Binding}" Style="{StaticResource PaneStyle}" />
172
-
173
87
  </DockPanel>
174
-
175
88
  </Window>
176
-
177
- ```
89
+ ```
178
-
179
-
180
-
90
+
181
- ```C#
91
+ ```cs
182
-
183
92
  using CommunityToolkit.Mvvm.ComponentModel;
184
-
185
93
  using CommunityToolkit.Mvvm.Input;
186
-
187
94
  using System.Collections.ObjectModel;
188
-
189
95
  using System.Diagnostics;
190
-
191
96
  using System.Text.Json;
192
-
193
97
  using System.Text.Json.Serialization;
194
-
195
98
  using System.Windows;
196
-
197
99
  using System.Windows.Controls;
198
-
199
100
  using System.Windows.Media;
200
101
 
201
-
202
-
203
102
  namespace Questions355107
204
-
205
103
  {
206
-
207
104
  public class WindowModel : ObservableObject
208
-
209
- {
105
+ {
210
-
211
106
  public double Top { get => top; set => SetProperty(ref top, value); }
212
-
213
107
  private double top = double.NaN;
214
-
215
108
  public double Left { get => left; set => SetProperty(ref left, value); }
216
-
217
109
  private double left = double.NaN;
218
-
219
110
  public double Width { get => width; set => SetProperty(ref width, value); }
220
-
221
111
  private double width = 800;
222
-
223
112
  public double Height { get => height; set => SetProperty(ref height, value); }
224
-
225
113
  private double height = 450;
226
-
227
- }
114
+ }
228
-
229
-
230
115
 
231
116
  // 分割割合保存復元用 GridSplitter1本に対し文字列2つ
232
-
233
117
  // 大元(Settings)から変わるので、SetPropertyしなくてよかった模様
234
-
235
118
  // ついでに配列化してすっきり
236
-
237
119
  public class SplitterModel
238
-
239
- {
120
+ {
240
-
241
121
  public string[] TwoPane { get; set; } = { "1*", "1*", };
242
-
243
122
  public string[] ThreePane { get; set; } = { "3*", "2*", "1*", "1*" };
244
-
245
123
  public string[] FourPane { get; set; } = { "1*", "1*", "1*", "1*", };
246
-
247
- }
124
+ }
248
-
249
-
250
125
 
251
126
  public class SettingsModel
252
-
253
- {
127
+ {
254
-
255
128
  public WindowModel Window { get; set; } = new();
256
-
257
129
  public SplitterModel Splitter { get; set; } = new();
258
-
259
- }
130
+ }
260
-
261
-
262
131
 
263
132
  public class ContentModel
264
-
265
- {
133
+ {
266
-
267
134
  public string Text { get; set; }
268
135
 
269
-
270
-
271
136
  // 実際に欲しい数値はここ
272
-
273
137
  // Viewから値が入るのみ コードから値を入れてもViewには反映しないので注意
274
-
275
138
  public double Width { get; set; }
276
-
277
139
  public double Height { get; set; }
278
-
279
140
  public Brush Brush { get; set; }
280
-
281
- }
141
+ }
282
-
283
-
284
142
 
285
143
  public class ViewModel : ObservableObject
286
-
287
- {
144
+ {
288
-
289
145
  public SettingsModel Settings { get => settings; set => SetProperty(ref settings, value); }
290
-
291
146
  private SettingsModel settings;
292
147
 
293
-
294
-
295
148
  public int[] Panes { get; } = { 1, 2, 3, 4, };
296
-
297
149
  public int SelPane { get => selPane; set => SetProperty(ref selPane, value); }
298
-
299
150
  private int selPane = 3;
300
-
301
151
  public ObservableCollection<ContentModel> Contents { get; }
302
152
 
303
-
304
-
305
153
  public RelayCommand Save { get; }
306
-
307
154
  public RelayCommand Load { get; }
308
-
309
155
  public RelayCommand View { get; }
310
156
 
311
-
312
-
313
157
  private string settingsJson;
314
-
315
158
  private readonly JsonSerializerOptions options = new()
316
-
317
159
  {
318
-
319
160
  NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals,
320
-
321
161
  WriteIndented = true,
322
-
323
162
  };
324
163
 
325
-
326
-
327
164
  public ViewModel()
328
-
329
165
  {
330
-
331
166
  Settings = new();
332
-
333
167
  Contents = new()
334
-
335
- {
168
+ {
336
-
337
169
  new() { Text = "①", Brush = Brushes.LightCoral, },
338
-
339
170
  new() { Text = "②", Brush = Brushes.LightGreen, },
340
-
341
171
  new() { Text = "③", Brush = Brushes.SkyBlue, },
342
-
343
172
  new() { Text = "④", Brush = Brushes.Pink, },
344
-
345
173
  };
346
-
347
174
  Save = new(() =>
348
-
349
- {
175
+ {
350
-
351
176
  settingsJson = JsonSerializer.Serialize(Settings, options);
352
-
353
177
  Debug.WriteLine(settingsJson);
354
-
355
178
  });
356
-
357
179
  Load = new(() =>
358
-
359
- {
180
+ {
360
-
361
181
  Settings = null;
362
-
363
182
  Settings = JsonSerializer.Deserialize<SettingsModel>(settingsJson, options);
364
-
365
183
  });
366
-
367
184
  View = new(() =>
368
-
369
- {
185
+ {
370
-
371
186
  foreach (var c in Contents)
372
-
373
187
  Debug.WriteLine($"{c.Text}: {c.Width:f2}, {c.Height:f2}");
374
-
375
188
  });
376
-
377
189
  Save.Execute(null);
378
-
379
190
  }
380
-
381
- }
191
+ }
382
-
383
-
384
192
 
385
193
  public partial class MainWindow : Window
386
-
387
- {
194
+ {
388
-
389
195
  public MainWindow() => InitializeComponent();
390
-
391
196
  private void Border_SizeChanged(object sender, SizeChangedEventArgs e)
392
-
393
197
  {
394
-
395
198
  if (sender is Border b && b.DataContext is ContentModel c)
396
-
397
- {
199
+ {
398
-
399
200
  c.Width = b.ActualWidth;
400
-
401
201
  c.Height = b.ActualHeight;
402
-
403
202
  }
404
-
405
203
  }
406
-
407
- }
204
+ }
408
-
409
205
  }
410
-
411
- ```
206
+ ```
412
-
413
-
414
-
207
+
415
- ```xaml
208
+ ```xml:TwoPane.xaml
416
-
417
209
  <UserControl x:Class="Questions355107.TwoPane" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
418
-
419
210
  <Grid>
420
-
421
211
  <Grid.ColumnDefinitions>
422
-
423
212
  <ColumnDefinition Width="{Binding Settings.Splitter.TwoPane[0], Mode=TwoWay}" />
424
-
425
213
  <ColumnDefinition Width="5" />
426
-
427
214
  <ColumnDefinition Width="{Binding Settings.Splitter.TwoPane[1], Mode=TwoWay}" />
428
-
429
215
  </Grid.ColumnDefinitions>
430
-
431
216
  <ContentControl Content="{Binding Contents[0]}" />
432
-
433
217
  <GridSplitter Grid.Column="1" />
434
-
435
218
  <ContentControl Grid.Column="2" Content="{Binding Contents[1]}" />
436
-
437
219
  </Grid>
438
-
439
220
  </UserControl>
440
-
441
- ```
221
+ ```
442
-
443
-
444
-
222
+
445
- ```xaml
223
+ ```xml:ThreePane.xaml
446
-
447
224
  <UserControl x:Class="Questions355107.ThreePane" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
448
-
449
225
  <Grid>
450
-
451
226
  <Grid.ColumnDefinitions>
452
-
453
227
  <ColumnDefinition Width="{Binding Settings.Splitter.ThreePane[0], Mode=TwoWay}" />
454
-
455
228
  <ColumnDefinition Width="5" />
456
-
457
229
  <ColumnDefinition Width="{Binding Settings.Splitter.ThreePane[1], Mode=TwoWay}" />
458
-
459
230
  </Grid.ColumnDefinitions>
460
-
461
231
  <ContentControl Content="{Binding Contents[0]}" />
462
-
463
232
  <GridSplitter Grid.Column="1" />
464
-
465
233
  <Grid Grid.Column="2">
466
-
467
234
  <Grid.RowDefinitions>
468
-
469
235
  <RowDefinition Height="{Binding Settings.Splitter.ThreePane[2], Mode=TwoWay}" />
470
-
471
236
  <RowDefinition Height="5" />
472
-
473
237
  <RowDefinition Height="{Binding Settings.Splitter.ThreePane[3], Mode=TwoWay}" />
474
-
475
238
  </Grid.RowDefinitions>
476
-
477
239
  <ContentControl Content="{Binding Contents[1]}" />
478
-
479
240
  <GridSplitter Grid.Row="1" />
480
-
481
241
  <ContentControl Grid.Row="2" Content="{Binding Contents[2]}" />
482
-
483
242
  </Grid>
484
-
485
243
  </Grid>
486
-
487
244
  </UserControl>
488
-
489
- ```
245
+ ```
490
-
491
-
492
-
246
+
493
- ```xaml
247
+ ```xml:FourPane.xaml
494
-
495
248
  <UserControl x:Class="Questions355107.FourPane" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
496
-
497
249
  <Grid>
498
-
499
250
  <Grid.ColumnDefinitions>
500
-
501
251
  <ColumnDefinition Width="{Binding Settings.Splitter.FourPane[0], Mode=TwoWay}" />
502
-
503
252
  <ColumnDefinition Width="5" />
504
-
505
253
  <ColumnDefinition Width="{Binding Settings.Splitter.FourPane[1], Mode=TwoWay}" />
506
-
507
254
  </Grid.ColumnDefinitions>
508
-
509
255
  <Grid.RowDefinitions>
510
-
511
256
  <RowDefinition Height="{Binding Settings.Splitter.FourPane[2], Mode=TwoWay}" />
512
-
513
257
  <RowDefinition Height="5" />
514
-
515
258
  <RowDefinition Height="{Binding Settings.Splitter.FourPane[3], Mode=TwoWay}" />
516
-
517
259
  </Grid.RowDefinitions>
518
-
519
260
  <ContentControl Content="{Binding Contents[0]}" />
520
-
521
261
  <ContentControl Grid.Row="2" Content="{Binding Contents[1]}" />
522
-
523
262
  <ContentControl Grid.Column="2" Content="{Binding Contents[2]}" />
524
-
525
263
  <ContentControl Grid.Row="2" Grid.Column="2" Content="{Binding Contents[3]}" />
526
-
527
264
  <GridSplitter Grid.RowSpan="3" Grid.Column="1" />
528
-
529
265
  <GridSplitter Grid.Row="1" Grid.ColumnSpan="3" />
530
-
531
266
  </Grid>
532
-
533
267
  </UserControl>
534
-
535
- ```
268
+ ```
536
-
537
269
  ![アプリ画像](9d7dc049e87998b40fb61771dda21579.png)