teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

動画追加

2024/08/31 16:35

投稿

TN8001
TN8001

スコア10111

answer CHANGED
@@ -1,4 +1,4 @@
1
- まずMessageBoxWindowはクライアント領域内を完全に覆うのでしょうから単に、
1
+ まず`MessageBoxWindow`はクライアント領域内を完全に覆うのでしょうから単に、
2
2
  ```xml
3
3
  <Window>
4
4
  <Grid>
@@ -16,8 +16,24 @@
16
16
 
17
17
  ---
18
18
 
19
+ ```xml:MainWindow.xaml
19
- MainWindow
20
+ <Window
21
+ x:Class="Questions227100.MainWindow"
22
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
23
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
24
+ xmlns:window="clr-namespace:Questions227100"
25
+ Width="800"
26
+ Height="450">
27
+ <Grid>
28
+ <window:MessageBoxWindow x:Name="messageBoxWindow" />
29
+
30
+ <!--<window:LanguageMenuWindow x:Name="LanguageMenuWindow" />-->
31
+ <!-- ↑の代わりにただのボタン -->
32
+ <Button Click="Button_Click" Content="何かを実行" />
33
+ </Grid>
34
+ </Window>
20
- ```cs
35
+ ```
36
+ ```cs:MainWindow.xaml.cs
21
37
  using System.Diagnostics;
22
38
  using System.Windows;
23
39
 
@@ -38,28 +54,56 @@
38
54
  }
39
55
  ```
40
56
 
57
+ ---
58
+
41
- ```xml
59
+ ```xml:MessageBoxWindow.xaml
42
- <Window
60
+ <UserControl
43
- x:Class="Questions227100.MainWindow"
61
+ x:Class="Questions227100.MessageBoxWindow"
44
62
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
45
63
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
64
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
65
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
+ d:DesignHeight="450"
67
+ d:DesignWidth="800"
46
- xmlns:window="clr-namespace:Questions227100"
68
+ Background="#4C000000"
69
+ mc:Ignorable="d">
70
+ <Border
47
- Width="800"
71
+ MaxWidth="500"
48
- Height="450">
72
+ MaxHeight="300"
73
+ VerticalAlignment="Center"
74
+ Background="#FFEEEEEE"
75
+ BorderBrush="#FF0071EF"
76
+ BorderThickness="0,5,0,0">
49
- <Grid>
77
+ <Grid>
78
+ <Grid.RowDefinitions>
79
+ <RowDefinition />
50
- <window:MessageBoxWindow x:Name="messageBoxWindow" />
80
+ <RowDefinition Height="Auto" />
51
-
81
+ </Grid.RowDefinitions>
82
+ <TextBlock
83
+ x:Name="MessageText"
84
+ Margin="10"
85
+ FontWeight="Bold"
86
+ TextWrapping="Wrap" />
87
+ <StackPanel
88
+ Grid.Row="1"
89
+ HorizontalAlignment="Right"
90
+ Orientation="Horizontal">
91
+ <Button
92
+ MinWidth="90"
93
+ Margin="10"
94
+ Click="ButtonOK_Click"
95
+ Content="OK" />
96
+ <Button
97
+ MinWidth="90"
98
+ Margin="10"
99
+ Click="ButtonCancel_Click"
52
- <!--<window:LanguageMenuWindow x:Name="LanguageMenuWindow" />-->
100
+ Content="Cancel" />
53
- <!-- ↑の代わりにただのボタン -->
101
+ </StackPanel>
54
- <Button Click="Button_Click" Content="何かを実行" />
55
- </Grid>
102
+ </Grid>
56
- </Window>
103
+ </Border>
104
+ </UserControl>
57
105
  ```
58
-
59
- ---
60
-
61
- MessageBoxWindow
106
+ ```cs:MessageBoxWindow.xaml.cs
62
- ```cs
63
107
  using System;
64
108
  using System.Collections.Generic;
65
109
  using System.Linq;
@@ -140,55 +184,8 @@
140
184
  }
141
185
  }
142
186
  ```
187
+ ![アプリ動画](https://ddjkaamml8q8x.cloudfront.net/questions/2024-09-01/e9e72aa7-c2a4-4874-bf48-7bcb6cd49ebf.gif)
143
188
 
144
- ```xml
145
- <UserControl
146
- x:Class="Questions227100.MessageBoxWindow"
147
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
148
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
149
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
150
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
151
- d:DesignHeight="450"
152
- d:DesignWidth="800"
153
- Background="#4C000000"
154
- mc:Ignorable="d">
155
- <Border
156
- MaxWidth="500"
157
- MaxHeight="300"
158
- VerticalAlignment="Center"
159
- Background="#FFEEEEEE"
160
- BorderBrush="#FF0071EF"
161
- BorderThickness="0,5,0,0">
162
- <Grid>
163
- <Grid.RowDefinitions>
164
- <RowDefinition />
165
- <RowDefinition Height="Auto" />
166
- </Grid.RowDefinitions>
167
- <TextBlock
168
- x:Name="MessageText"
169
- Margin="10"
170
- FontWeight="Bold"
171
- TextWrapping="Wrap" />
172
- <StackPanel
173
- Grid.Row="1"
174
- HorizontalAlignment="Right"
175
- Orientation="Horizontal">
176
- <Button
177
- MinWidth="90"
178
- Margin="10"
179
- Click="ButtonOK_Click"
180
- Content="OK" />
181
- <Button
182
- MinWidth="90"
183
- Margin="10"
184
- Click="ButtonCancel_Click"
185
- Content="Cancel" />
186
- </StackPanel>
187
- </Grid>
188
- </Border>
189
- </UserControl>
190
- ```
191
-
192
189
  ---
193
190
 
194
191
  ダイアログだけかっこよくなってもバランスがありますので、

1

リンク切れ

2022/09/15 09:53

投稿

TN8001
TN8001

スコア10111

answer CHANGED
@@ -1,199 +1,199 @@
1
- まずMessageBoxWindowはクライアント領域内を完全に覆うのでしょうから単に、
2
- ```xaml
3
- <Window>
4
- <Grid>
5
- <window:MessageBoxWindow />
6
- <window:LanguageMenuWindow />
7
- </Grid>
8
- </Window>
9
- ```
10
- こういう形でいいんじゃないでしょうか。
11
-
12
- バインディングをうまく使えば、割とすっきりできそうにも思うのですが、
13
- > MessageBoxResult flag = MessageBox.Show("OKですか?");
14
-
15
- こういうイメージとのことでちょっと無理やり感もありますが、今のコードをあまり壊さない形でやってみました。
16
-
17
- ---
18
-
19
- MainWindow
20
- ```C#
21
- using System.Diagnostics;
22
- using System.Windows;
23
-
24
- namespace Questions227100
25
- {
26
- public partial class MainWindow : Window
27
- {
28
- public MainWindow() => InitializeComponent();
29
-
30
- private async void Button_Click(object sender, RoutedEventArgs e)
31
- {
32
- var result = await MessageBoxWindow.ShowAsync(this, "いいんですね?");
33
-
34
- if(result == MessageBoxResult.OK) Debug.WriteLine("OK");
35
- if(result == MessageBoxResult.Cancel) Debug.WriteLine("Cancel");
36
- }
37
- }
38
- }
39
- ```
40
-
41
- ```xaml
42
- <Window
43
- x:Class="Questions227100.MainWindow"
44
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
45
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
46
- xmlns:window="clr-namespace:Questions227100"
47
- Width="800"
48
- Height="450">
49
- <Grid>
50
- <window:MessageBoxWindow x:Name="messageBoxWindow" />
51
-
52
- <!--<window:LanguageMenuWindow x:Name="LanguageMenuWindow" />-->
53
- <!-- ↑の代わりにただのボタン -->
54
- <Button Click="Button_Click" Content="何かを実行" />
55
- </Grid>
56
- </Window>
57
- ```
58
-
59
- ---
60
-
61
- MessageBoxWindow
62
- ```C#
63
- using System;
64
- using System.Collections.Generic;
65
- using System.Linq;
66
- using System.Threading.Tasks;
67
- using System.Windows;
68
- using System.Windows.Controls;
69
- using System.Windows.Media;
70
-
71
- namespace Questions227100
72
- {
73
- public partial class MessageBoxWindow : UserControl
74
- {
75
- private TaskCompletionSource<MessageBoxResult> taskCompletionSource;
76
-
77
- public MessageBoxWindow()
78
- {
79
- InitializeComponent();
80
- SetValue(Panel.ZIndexProperty, 10);
81
- Visibility = Visibility.Hidden;
82
- }
83
-
84
- public static Task<MessageBoxResult> ShowAsync(UIElement element, string message)
85
- {
86
- var win = Window.GetWindow(element);
87
- var msgbox = win.Descendants<MessageBoxWindow>().FirstOrDefault();
88
- if(msgbox == null) throw new InvalidOperationException("MessageBoxWindowが見つかりません。");
89
- return msgbox.ShowAsync(message);
90
- }
91
- public Task<MessageBoxResult> ShowAsync(string message)
92
- {
93
- MessageText.Text = message;
94
- Visibility = Visibility.Visible;
95
- taskCompletionSource = new TaskCompletionSource<MessageBoxResult>();
96
- return taskCompletionSource.Task;
97
- }
98
-
99
- private void ButtonOK_Click(object sender, RoutedEventArgs e)
100
- {
101
- Visibility = Visibility.Hidden;
102
- taskCompletionSource.SetResult(MessageBoxResult.OK);
103
- }
104
- private void ButtonCancel_Click(object sender, RoutedEventArgs e)
105
- {
106
- Visibility = Visibility.Hidden;
107
- taskCompletionSource.SetResult(MessageBoxResult.Cancel);
108
- }
109
- }
110
-
111
-
112
-
113
- //https://blog.xin9le.net/entry/2013/10/29/222336
114
- internal static class DependencyObjectExtensions
115
- {
116
- public static IEnumerable<DependencyObject> Children(this DependencyObject obj)
117
- {
118
- if(obj == null) throw new ArgumentNullException(nameof(obj));
119
- var count = VisualTreeHelper.GetChildrenCount(obj);
120
- if(count == 0) yield break;
121
- for(var i = 0; i < count; i++)
122
- {
123
- var child = VisualTreeHelper.GetChild(obj, i);
124
- if(child != null) yield return child;
125
- }
126
- }
127
- public static IEnumerable<DependencyObject> Descendants(this DependencyObject obj)
128
- {
129
- if(obj == null) throw new ArgumentNullException(nameof(obj));
130
- foreach(var child in obj.Children())
131
- {
132
- yield return child;
133
- foreach(var grandChild in child.Descendants()) yield return grandChild;
134
- }
135
- }
136
- public static IEnumerable<T> Children<T>(this DependencyObject obj)
137
- where T : DependencyObject => obj.Children().OfType<T>();
138
- public static IEnumerable<T> Descendants<T>(this DependencyObject obj)
139
- where T : DependencyObject => obj.Descendants().OfType<T>();
140
- }
141
- }
142
- ```
143
-
144
- ```xaml
145
- <UserControl
146
- x:Class="Questions227100.MessageBoxWindow"
147
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
148
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
149
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
150
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
151
- d:DesignHeight="450"
152
- d:DesignWidth="800"
153
- Background="#4C000000"
154
- mc:Ignorable="d">
155
- <Border
156
- MaxWidth="500"
157
- MaxHeight="300"
158
- VerticalAlignment="Center"
159
- Background="#FFEEEEEE"
160
- BorderBrush="#FF0071EF"
161
- BorderThickness="0,5,0,0">
162
- <Grid>
163
- <Grid.RowDefinitions>
164
- <RowDefinition />
165
- <RowDefinition Height="Auto" />
166
- </Grid.RowDefinitions>
167
- <TextBlock
168
- x:Name="MessageText"
169
- Margin="10"
170
- FontWeight="Bold"
171
- TextWrapping="Wrap" />
172
- <StackPanel
173
- Grid.Row="1"
174
- HorizontalAlignment="Right"
175
- Orientation="Horizontal">
176
- <Button
177
- MinWidth="90"
178
- Margin="10"
179
- Click="ButtonOK_Click"
180
- Content="OK" />
181
- <Button
182
- MinWidth="90"
183
- Margin="10"
184
- Click="ButtonCancel_Click"
185
- Content="Cancel" />
186
- </StackPanel>
187
- </Grid>
188
- </Border>
189
- </UserControl>
190
- ```
191
-
192
- ---
193
-
194
- ダイアログだけかっこよくなってもバランスがありますので、
195
- [mahapps.metro - Dialogs](https://mahapps.com/controls/dialogs.html)
196
- [Material Design In XAML Toolkit - Dialogs](https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/wiki/Dialogs)
197
-
198
- こういったものに乗ってしまえば、すでに同様のものが用意されています。
1
+ まずMessageBoxWindowはクライアント領域内を完全に覆うのでしょうから単に、
2
+ ```xml
3
+ <Window>
4
+ <Grid>
5
+ <window:MessageBoxWindow />
6
+ <window:LanguageMenuWindow />
7
+ </Grid>
8
+ </Window>
9
+ ```
10
+ こういう形でいいんじゃないでしょうか。
11
+
12
+ バインディングをうまく使えば、割とすっきりできそうにも思うのですが、
13
+ > MessageBoxResult flag = MessageBox.Show("OKですか?");
14
+
15
+ こういうイメージとのことでちょっと無理やり感もありますが、今のコードをあまり壊さない形でやってみました。
16
+
17
+ ---
18
+
19
+ MainWindow
20
+ ```cs
21
+ using System.Diagnostics;
22
+ using System.Windows;
23
+
24
+ namespace Questions227100
25
+ {
26
+ public partial class MainWindow : Window
27
+ {
28
+ public MainWindow() => InitializeComponent();
29
+
30
+ private async void Button_Click(object sender, RoutedEventArgs e)
31
+ {
32
+ var result = await MessageBoxWindow.ShowAsync(this, "いいんですね?");
33
+
34
+ if(result == MessageBoxResult.OK) Debug.WriteLine("OK");
35
+ if(result == MessageBoxResult.Cancel) Debug.WriteLine("Cancel");
36
+ }
37
+ }
38
+ }
39
+ ```
40
+
41
+ ```xml
42
+ <Window
43
+ x:Class="Questions227100.MainWindow"
44
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
45
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
46
+ xmlns:window="clr-namespace:Questions227100"
47
+ Width="800"
48
+ Height="450">
49
+ <Grid>
50
+ <window:MessageBoxWindow x:Name="messageBoxWindow" />
51
+
52
+ <!--<window:LanguageMenuWindow x:Name="LanguageMenuWindow" />-->
53
+ <!-- ↑の代わりにただのボタン -->
54
+ <Button Click="Button_Click" Content="何かを実行" />
55
+ </Grid>
56
+ </Window>
57
+ ```
58
+
59
+ ---
60
+
61
+ MessageBoxWindow
62
+ ```cs
63
+ using System;
64
+ using System.Collections.Generic;
65
+ using System.Linq;
66
+ using System.Threading.Tasks;
67
+ using System.Windows;
68
+ using System.Windows.Controls;
69
+ using System.Windows.Media;
70
+
71
+ namespace Questions227100
72
+ {
73
+ public partial class MessageBoxWindow : UserControl
74
+ {
75
+ private TaskCompletionSource<MessageBoxResult> taskCompletionSource;
76
+
77
+ public MessageBoxWindow()
78
+ {
79
+ InitializeComponent();
80
+ SetValue(Panel.ZIndexProperty, 10);
81
+ Visibility = Visibility.Hidden;
82
+ }
83
+
84
+ public static Task<MessageBoxResult> ShowAsync(UIElement element, string message)
85
+ {
86
+ var win = Window.GetWindow(element);
87
+ var msgbox = win.Descendants<MessageBoxWindow>().FirstOrDefault();
88
+ if(msgbox == null) throw new InvalidOperationException("MessageBoxWindowが見つかりません。");
89
+ return msgbox.ShowAsync(message);
90
+ }
91
+ public Task<MessageBoxResult> ShowAsync(string message)
92
+ {
93
+ MessageText.Text = message;
94
+ Visibility = Visibility.Visible;
95
+ taskCompletionSource = new TaskCompletionSource<MessageBoxResult>();
96
+ return taskCompletionSource.Task;
97
+ }
98
+
99
+ private void ButtonOK_Click(object sender, RoutedEventArgs e)
100
+ {
101
+ Visibility = Visibility.Hidden;
102
+ taskCompletionSource.SetResult(MessageBoxResult.OK);
103
+ }
104
+ private void ButtonCancel_Click(object sender, RoutedEventArgs e)
105
+ {
106
+ Visibility = Visibility.Hidden;
107
+ taskCompletionSource.SetResult(MessageBoxResult.Cancel);
108
+ }
109
+ }
110
+
111
+
112
+
113
+ //https://blog.xin9le.net/entry/2013/10/29/222336
114
+ internal static class DependencyObjectExtensions
115
+ {
116
+ public static IEnumerable<DependencyObject> Children(this DependencyObject obj)
117
+ {
118
+ if(obj == null) throw new ArgumentNullException(nameof(obj));
119
+ var count = VisualTreeHelper.GetChildrenCount(obj);
120
+ if(count == 0) yield break;
121
+ for(var i = 0; i < count; i++)
122
+ {
123
+ var child = VisualTreeHelper.GetChild(obj, i);
124
+ if(child != null) yield return child;
125
+ }
126
+ }
127
+ public static IEnumerable<DependencyObject> Descendants(this DependencyObject obj)
128
+ {
129
+ if(obj == null) throw new ArgumentNullException(nameof(obj));
130
+ foreach(var child in obj.Children())
131
+ {
132
+ yield return child;
133
+ foreach(var grandChild in child.Descendants()) yield return grandChild;
134
+ }
135
+ }
136
+ public static IEnumerable<T> Children<T>(this DependencyObject obj)
137
+ where T : DependencyObject => obj.Children().OfType<T>();
138
+ public static IEnumerable<T> Descendants<T>(this DependencyObject obj)
139
+ where T : DependencyObject => obj.Descendants().OfType<T>();
140
+ }
141
+ }
142
+ ```
143
+
144
+ ```xml
145
+ <UserControl
146
+ x:Class="Questions227100.MessageBoxWindow"
147
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
148
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
149
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
150
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
151
+ d:DesignHeight="450"
152
+ d:DesignWidth="800"
153
+ Background="#4C000000"
154
+ mc:Ignorable="d">
155
+ <Border
156
+ MaxWidth="500"
157
+ MaxHeight="300"
158
+ VerticalAlignment="Center"
159
+ Background="#FFEEEEEE"
160
+ BorderBrush="#FF0071EF"
161
+ BorderThickness="0,5,0,0">
162
+ <Grid>
163
+ <Grid.RowDefinitions>
164
+ <RowDefinition />
165
+ <RowDefinition Height="Auto" />
166
+ </Grid.RowDefinitions>
167
+ <TextBlock
168
+ x:Name="MessageText"
169
+ Margin="10"
170
+ FontWeight="Bold"
171
+ TextWrapping="Wrap" />
172
+ <StackPanel
173
+ Grid.Row="1"
174
+ HorizontalAlignment="Right"
175
+ Orientation="Horizontal">
176
+ <Button
177
+ MinWidth="90"
178
+ Margin="10"
179
+ Click="ButtonOK_Click"
180
+ Content="OK" />
181
+ <Button
182
+ MinWidth="90"
183
+ Margin="10"
184
+ Click="ButtonCancel_Click"
185
+ Content="Cancel" />
186
+ </StackPanel>
187
+ </Grid>
188
+ </Border>
189
+ </UserControl>
190
+ ```
191
+
192
+ ---
193
+
194
+ ダイアログだけかっこよくなってもバランスがありますので、
195
+ [MahApps.Metro - Dialogs](https://mahapps.com/docs/dialogs/)
196
+ [Material Design In XAML Toolkit - Dialogs](https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/wiki/Dialogs)
197
+
198
+ こういったものに乗ってしまえば、すでに同様のものが用意されています。
199
199
  ただ導入するにもまた調整がいるので、是非にとまでは言えません。