回答編集履歴

3

見直しキャンペーン中

2023/07/23 05:05

投稿

TN8001
TN8001

スコア9396

test CHANGED
@@ -1,127 +1,64 @@
1
1
  確かにあまりいい方法はなさそうです。
2
-
3
-
4
2
 
5
3
  > コントロールの大きさを計算して、MinHight、MinWidthを設定する
6
4
 
7
-
8
-
9
5
  一時的に`SizeToContent="WidthAndHeight"`にすれば計算する必要はありません。
10
-
11
6
  その時のサイズを入れるだけです。
12
-
13
7
  しかしタイトルバーの高さ等はユーザー設定によるので、環境によってずれてしまいます。
14
-
15
8
  `SizeToContent="WidthAndHeight"`を入れたままでいい(初期サイズがぴったりサイズでいい)なら`Loaded`イベントあたりで`MinWidth = Width`等とできます。
16
9
 
17
-
18
-
19
- ```xaml
10
+ ```xml
20
-
21
11
  <Window
22
-
23
12
  x:Class="Questions288908.MainWindow"
24
-
25
13
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
26
-
27
14
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
28
-
29
15
  Loaded="Window_Loaded"
30
-
31
16
  SizeToContent="WidthAndHeight">
32
-
33
17
  <Window.Resources>
34
-
35
18
  <Style TargetType="Button">
36
-
37
19
  <Setter Property="Width" Value="200" />
38
-
39
20
  <Setter Property="Height" Value="150" />
40
-
41
21
  <Setter Property="Margin" Value="20" />
42
-
43
22
  <Setter Property="Content" Value="Button" />
44
-
45
23
  </Style>
46
-
47
24
  </Window.Resources>
48
-
49
25
  <StackPanel>
50
-
51
26
  <StackPanel Orientation="Horizontal">
52
-
53
27
  <Button />
54
-
55
28
  <Button />
56
-
57
29
  <Button />
58
-
59
30
  </StackPanel>
60
-
61
31
  <StackPanel Orientation="Horizontal">
62
-
63
32
  <Button />
64
-
65
33
  <Button />
66
-
67
34
  <Button />
68
-
69
35
  </StackPanel>
70
-
71
36
  </StackPanel>
72
-
73
37
  </Window>
74
-
75
38
  ```
76
39
 
77
-
78
-
79
- ```C#
40
+ ```cs
80
-
81
41
  using System.Windows;
82
42
 
83
-
84
-
85
43
  namespace Questions288908
86
-
87
44
  {
88
-
89
45
  public partial class MainWindow : Window
90
-
91
46
  {
92
-
93
47
  public MainWindow()
94
-
95
48
  {
96
-
97
49
  InitializeComponent();
98
-
99
50
  }
100
51
 
101
-
102
-
103
52
  private void Window_Loaded(object sender, RoutedEventArgs e)
104
-
105
53
  {
106
-
107
54
  MinWidth = Width;
108
-
109
55
  MinHeight = Height;
110
-
111
56
  }
112
-
113
57
  }
114
-
115
58
  }
116
-
117
59
  ```
118
-
119
60
  ---
120
61
 
121
-
122
-
123
62
  そもそも論なのですがWPFでのデザインでは、コントロールに固定サイズを入れることはせずに、ウィンドウサイズに合わせて伸縮させるのが一般的です。
124
-
125
63
  もちろん操作不能に小さくならないように、ウィンドウに`MinWidth`等を入れることは普通です。
126
-
127
64
  その際は中身の大きさを積み重ねてボトムアップで計算しているのではなく、ウィンドウの最小サイズはこんなもんかな?とトップダウンで決めているような気がします(マイクロソフトのアプリ等を見る限り。もっと小さくできそうなのに小さくならない

2

1行多かったw

2020/09/01 10:51

投稿

TN8001
TN8001

スコア9396

test CHANGED
@@ -47,16 +47,6 @@
47
47
  </Window.Resources>
48
48
 
49
49
  <StackPanel>
50
-
51
- <StackPanel Orientation="Horizontal">
52
-
53
- <Button />
54
-
55
- <Button />
56
-
57
- <Button />
58
-
59
- </StackPanel>
60
50
 
61
51
  <StackPanel Orientation="Horizontal">
62
52
 

1

コード追加

2020/09/01 10:51

投稿

TN8001
TN8001

スコア9396

test CHANGED
@@ -16,6 +16,116 @@
16
16
 
17
17
 
18
18
 
19
+ ```xaml
20
+
21
+ <Window
22
+
23
+ x:Class="Questions288908.MainWindow"
24
+
25
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
26
+
27
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
28
+
29
+ Loaded="Window_Loaded"
30
+
31
+ SizeToContent="WidthAndHeight">
32
+
33
+ <Window.Resources>
34
+
35
+ <Style TargetType="Button">
36
+
37
+ <Setter Property="Width" Value="200" />
38
+
39
+ <Setter Property="Height" Value="150" />
40
+
41
+ <Setter Property="Margin" Value="20" />
42
+
43
+ <Setter Property="Content" Value="Button" />
44
+
45
+ </Style>
46
+
47
+ </Window.Resources>
48
+
49
+ <StackPanel>
50
+
51
+ <StackPanel Orientation="Horizontal">
52
+
53
+ <Button />
54
+
55
+ <Button />
56
+
57
+ <Button />
58
+
59
+ </StackPanel>
60
+
61
+ <StackPanel Orientation="Horizontal">
62
+
63
+ <Button />
64
+
65
+ <Button />
66
+
67
+ <Button />
68
+
69
+ </StackPanel>
70
+
71
+ <StackPanel Orientation="Horizontal">
72
+
73
+ <Button />
74
+
75
+ <Button />
76
+
77
+ <Button />
78
+
79
+ </StackPanel>
80
+
81
+ </StackPanel>
82
+
83
+ </Window>
84
+
85
+ ```
86
+
87
+
88
+
89
+ ```C#
90
+
91
+ using System.Windows;
92
+
93
+
94
+
95
+ namespace Questions288908
96
+
97
+ {
98
+
99
+ public partial class MainWindow : Window
100
+
101
+ {
102
+
103
+ public MainWindow()
104
+
105
+ {
106
+
107
+ InitializeComponent();
108
+
109
+ }
110
+
111
+
112
+
113
+ private void Window_Loaded(object sender, RoutedEventArgs e)
114
+
115
+ {
116
+
117
+ MinWidth = Width;
118
+
119
+ MinHeight = Height;
120
+
121
+ }
122
+
123
+ }
124
+
125
+ }
126
+
127
+ ```
128
+
19
129
  ---
20
130
 
21
131