回答編集履歴

1

見直しキャンペーン中

2023/07/18 21:57

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,321 +1,157 @@
1
1
  `new AdditionalWindow()`や`new MainWindow()`が散見されますが、RyosukeKamimuraさんが意図している動作ではないと思います。
2
2
 
3
-
4
-
5
3
  newするということは別のウィンドウを作るということです。操作しようとしているものとは別のインスタンスです。
6
-
7
4
  `AdditionalWindow`が複数出る必要がない場合、ひとつだけnewし表示非表示で管理すると楽です。
8
5
 
9
-
10
-
11
- あと https://teratail.com/questions/233943 で[PropertyGrid](https://github.com/xceedsoftware/wpftoolkit/wiki/PropertyGrid)を使ったのは単にUIを作るのがだるかったからですが、
6
+ あと https://teratail.com/questions/233943 で[PropertyGrid](https://github.com/xceedsoftware/wpftoolkit/wiki/PropertyGrid)を使ったのは単にUIを作るのがだるかったからですが、自分で言うのもなんですが使いにくくないですか?^^;
12
-
13
- 自分で言うのもなんですが使いにくくないですか?^^;
14
-
15
-
16
7
 
17
8
  [IntegerUpDown](https://github.com/xceedsoftware/wpftoolkit/wiki/IntegerUpDown)とか[ColorPicker](https://github.com/xceedsoftware/wpftoolkit/wiki/ColorPicker)でUIを作っていただくのがベストですが、自分でもイライラしたのでPropertyGridを少々改良しました。
18
9
 
19
-
20
-
21
10
  変更したファイルのみ
22
-
23
- MainWindow.xaml.cs
11
+ ```cs:MainWindow.xaml.cs
24
-
25
- ```C#
26
-
27
12
  using System;
28
-
29
13
  using System.Windows;
30
-
31
14
  using System.Windows.Controls;
32
-
33
15
  using System.Windows.Input;
34
-
35
16
  using System.Windows.Media;
36
-
37
17
  using System.Windows.Media.Imaging;
38
-
39
18
  using Microsoft.Win32;
40
19
 
41
-
42
-
43
20
  namespace WpfApp4
44
-
45
21
  {
46
-
47
22
  public partial class MainWindow : Window
48
-
49
23
  {
50
-
51
24
  public string FilePath = null;
52
25
 
53
-
54
-
55
26
  private AdditionalWindow additionalWindow = new AdditionalWindow();
56
-
57
-
58
-
59
27
 
60
28
 
61
29
  public MainWindow() => InitializeComponent();
62
30
 
63
31
 
64
-
65
-
66
-
67
32
  private void button_Click(object sender, RoutedEventArgs e)
68
-
69
33
  {
70
-
71
34
  additionalWindow.Show();
72
-
73
35
  additionalWindow.Owner = this;
74
-
75
36
  }
76
37
 
77
-
78
-
79
38
  private void button_open_Click(object sender, RoutedEventArgs e)
80
-
81
39
  {
82
-
83
40
  OpenFileDialog openFileDialog = new OpenFileDialog();
84
-
85
41
  if(openFileDialog.ShowDialog() == true)
86
-
87
42
  {
88
-
89
43
  FilePath = openFileDialog.FileName;
90
-
91
44
  BitmapImage bitmapImage = new BitmapImage(new Uri(FilePath));
92
-
93
45
  imageBox.Source = bitmapImage;
94
-
95
46
  }
96
-
97
47
  }
98
48
 
99
-
100
-
101
49
  private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
102
-
103
50
  {
104
-
105
51
  if(e.ClickCount == 2)
106
-
107
52
  {
108
-
109
53
  var textBox = new TextBoxEx
110
-
111
54
  {
112
-
113
55
  Style = FindResource("TextBoxStyle") as Style,
114
-
115
56
  CanvasLeft = e.GetPosition(canvas).X,
116
-
117
57
  CanvasTop = e.GetPosition(canvas).Y,
118
-
119
58
  Foreground = new SolidColorBrush(Colors.Red), // 雑い^^;
120
-
121
59
  Background = new SolidColorBrush(),
122
-
123
60
  };
124
-
125
61
  textBox.GotFocus += TextBox_GotFocus;
126
-
127
62
  canvas.Children.Add(textBox);
128
-
129
63
  }
130
-
131
64
  }
132
65
 
133
-
134
-
135
66
  private void DelMenuItem_Click(object sender, RoutedEventArgs e)
136
-
137
67
  {
138
-
139
68
  if(sender is MenuItem menuItem)
140
-
141
69
  {
142
-
143
70
  if(menuItem.Parent is ContextMenu contextMenu)
144
-
145
71
  {
146
-
147
72
  if(contextMenu.PlacementTarget is TextBoxEx textBox)
148
-
149
73
  {
150
-
151
74
  additionalWindow.propertyGrid.SelectedObject = null;
152
-
153
75
  textBox.GotFocus -= TextBox_GotFocus;
154
-
155
76
  canvas.Children.Remove(textBox);
156
-
157
77
  }
158
-
159
78
  }
160
-
161
79
  }
162
-
163
80
  }
164
81
 
165
-
166
-
167
82
  private void TextBox_GotFocus(object sender, RoutedEventArgs e)
168
-
169
83
  {
170
-
171
84
  additionalWindow.propertyGrid.SelectedObject = sender;
172
-
173
85
  }
174
-
175
86
  }
176
-
177
87
  }
178
-
179
88
  ```
180
89
 
181
-
182
-
183
- AdditionalWindow.xaml
90
+ ```xml:AdditionalWindow.xaml
184
-
185
- ```xaml
186
-
187
91
  <Window
188
-
189
92
  x:Class="WpfApp4.AdditionalWindow"
190
-
191
93
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
192
-
193
94
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
194
-
195
95
  xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
196
-
197
96
  Title="AdditionalWindow"
198
-
199
97
  Width="350"
200
-
201
98
  Height="450"
202
-
203
99
  Closing="Window_Closing">
204
-
205
100
  <Grid>
206
-
207
101
  <xctk:PropertyGrid
208
-
209
102
  x:Name="propertyGrid"
210
-
211
103
  AutoGenerateProperties="False"
212
-
213
104
  ShowSearchBox="False"
214
-
215
105
  ShowSortOptions="False"
216
-
217
106
  ShowSummary="False"
218
-
219
107
  ShowTitle="False">
220
-
221
108
  <xctk:PropertyGrid.EditorDefinitions>
222
-
223
109
  <xctk:EditorDefinition>
224
-
225
110
  <xctk:EditorDefinition.PropertiesDefinitions>
226
-
227
111
  <xctk:PropertyDefinition Name="Background" />
228
-
229
112
  <xctk:PropertyDefinition Name="Foreground" />
230
-
231
113
  </xctk:EditorDefinition.PropertiesDefinitions>
232
-
233
114
  <xctk:EditorDefinition.EditorTemplate>
234
-
235
115
  <DataTemplate>
236
-
237
116
  <xctk:ColorPicker SelectedColor="{Binding Value.Color}" />
238
-
239
117
  </DataTemplate>
240
-
241
118
  </xctk:EditorDefinition.EditorTemplate>
242
-
243
119
  </xctk:EditorDefinition>
244
-
245
120
  </xctk:PropertyGrid.EditorDefinitions>
246
121
 
247
-
248
-
249
122
  <xctk:PropertyGrid.PropertyDefinitions>
250
-
251
123
  <xctk:PropertyDefinition Name="CanvasLeft" />
252
-
253
124
  <xctk:PropertyDefinition Name="CanvasTop" />
254
-
255
125
  <xctk:PropertyDefinition Name="Text" />
256
-
257
126
  <xctk:PropertyDefinition Name="Background" />
258
-
259
127
  <xctk:PropertyDefinition Name="FontFamily" />
260
-
261
128
  <xctk:PropertyDefinition Name="FontSize" />
262
-
263
129
  <xctk:PropertyDefinition Name="FontStretch" />
264
-
265
130
  <xctk:PropertyDefinition Name="FontStyle" />
266
-
267
131
  <xctk:PropertyDefinition Name="FontWeight" />
268
-
269
132
  <xctk:PropertyDefinition Name="Foreground" />
270
-
271
133
  </xctk:PropertyGrid.PropertyDefinitions>
272
-
273
134
  </xctk:PropertyGrid>
274
-
275
135
  </Grid>
276
-
277
136
  </Window>
278
-
279
137
  ```
280
138
 
281
-
282
-
283
- AdditionalWindow.xaml.cs
139
+ ```cs:AdditionalWindow.xaml.cs
284
-
285
- ```C#
286
-
287
140
  using System.ComponentModel;
288
-
289
141
  using System.Windows;
290
142
 
291
-
292
-
293
143
  namespace WpfApp4
294
-
295
144
  {
296
-
297
145
  public partial class AdditionalWindow : Window
298
-
299
146
  {
300
-
301
147
  public AdditionalWindow() => InitializeComponent();
302
148
 
303
-
304
-
305
149
  private void Window_Closing(object sender, CancelEventArgs e)
306
-
307
150
  {
308
-
309
151
  // 閉じずに非表示にする
310
-
311
152
  e.Cancel = true;
312
-
313
153
  Hide();
314
-
315
154
  }
316
-
317
155
  }
318
-
319
156
  }
320
-
321
157
  ```