回答編集履歴

1

見直しキャンペーン中

2023/07/18 21:08

投稿

TN8001
TN8001

スコア9350

test CHANGED
@@ -1,469 +1,235 @@
1
1
  マスをひとつひとつ作っていくことも可能ですが、XAML・C#コード共膨れ上がりそうなので`ItemsControl`と`DataTemplate`で作るようにしました。
2
-
3
2
  思ったようにバインディングできなかったりと作るのに一苦労ですが、できてしまえば色を何個でも追加できるのでこういった繰り返すものに最適です。
4
-
5
3
  ![イメージ説明](bdbc4f757145591c9fd0a80a4f9af7e0.png)
6
-
7
- ```xaml
4
+ ```xml
8
-
9
5
  <Window
10
-
11
6
  x:Class="Questions235362.MainWindow"
12
-
13
7
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
14
-
15
8
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
16
-
17
9
  xmlns:local="clr-namespace:Questions235362"
18
-
19
10
  MaxWidth="820"
20
-
21
11
  SizeToContent="WidthAndHeight">
22
-
23
12
  <Window.Resources>
24
-
25
13
  <local:BrushConverter x:Key="brushConverter" />
26
-
27
14
  </Window.Resources>
28
-
29
15
  <Grid>
30
-
31
16
  <Grid.RowDefinitions>
32
-
33
17
  <RowDefinition Height="Auto" />
34
-
35
18
  <RowDefinition />
36
-
37
19
  </Grid.RowDefinitions>
38
-
39
20
  <Label
40
-
41
21
  HorizontalAlignment="Center"
42
-
43
22
  VerticalAlignment="Center"
44
-
45
23
  Content="Color Pallet"
46
-
47
24
  FontSize="16" />
48
-
49
25
  <Button
50
-
51
26
  MinWidth="80"
52
-
53
27
  Margin="0,0,10,0"
54
-
55
28
  HorizontalAlignment="Right"
56
-
57
29
  VerticalAlignment="Center"
58
-
59
30
  Click="BtnOk_Click"
60
-
61
31
  Content="OK" />
62
32
 
63
-
64
-
65
33
  <ItemsControl x:Name="itemsControl" Grid.Row="1">
66
-
67
34
  <ItemsControl.ItemsPanel>
68
-
69
35
  <ItemsPanelTemplate>
70
-
71
36
  <WrapPanel Orientation="Horizontal" />
72
-
73
37
  </ItemsPanelTemplate>
74
-
75
38
  </ItemsControl.ItemsPanel>
76
-
77
39
  <ItemsControl.ItemTemplate>
78
-
79
40
  <DataTemplate>
80
-
81
41
  <Border
82
-
83
42
  Width="200"
84
-
85
43
  BorderBrush="Gray"
86
-
87
44
  BorderThickness="1">
88
-
89
45
  <Border.Background>
90
-
91
46
  <MultiBinding Converter="{StaticResource brushConverter}">
92
-
93
47
  <Binding ElementName="Sld10R" Path="Value" />
94
-
95
48
  <Binding ElementName="Sld10G" Path="Value" />
96
-
97
49
  <Binding ElementName="Sld10B" Path="Value" />
98
-
99
50
  </MultiBinding>
100
-
101
51
  </Border.Background>
102
-
103
52
  <StackPanel>
104
-
105
53
  <Grid>
106
-
107
54
  <Grid.ColumnDefinitions>
108
-
109
- <ColumnDefinition Width="Auto" />
55
+ <ColumnDefinition Width="Auto" />
110
-
111
56
  <ColumnDefinition />
112
-
113
- <ColumnDefinition Width="Auto" />
57
+ <ColumnDefinition Width="Auto" />
114
-
115
58
  </Grid.ColumnDefinitions>
116
-
117
59
  <Label VerticalAlignment="Center" Content="R" />
118
-
119
60
  <Slider
120
-
121
61
  Name="Sld10R"
122
-
123
62
  Grid.Column="1"
124
-
125
- VerticalAlignment="Center"
63
+ VerticalAlignment="Center"
126
-
127
64
  Maximum="255"
128
-
129
65
  Value="{Binding R}" />
130
-
131
66
  <TextBox
132
-
133
67
  Grid.Column="2"
134
-
135
68
  Width="50"
136
-
137
69
  Margin="5,0"
138
-
139
- VerticalAlignment="Center"
70
+ VerticalAlignment="Center"
140
-
141
71
  InputMethod.PreferredImeConversionMode="Alphanumeric"
142
-
143
72
  InputMethod.PreferredImeState="On"
144
-
145
73
  MaxLength="3"
146
-
147
74
  PreviewTextInput="TextBox_PreviewTextInput"
148
-
149
75
  Text="{Binding Value, ElementName=Sld10R, UpdateSourceTrigger=PropertyChanged}" />
150
-
151
76
  </Grid>
152
-
153
77
  <Grid>
154
-
155
78
  <Grid.ColumnDefinitions>
156
-
157
- <ColumnDefinition Width="Auto" />
79
+ <ColumnDefinition Width="Auto" />
158
-
159
80
  <ColumnDefinition />
160
-
161
- <ColumnDefinition Width="Auto" />
81
+ <ColumnDefinition Width="Auto" />
162
-
163
82
  </Grid.ColumnDefinitions>
164
-
165
83
  <Label VerticalAlignment="Center" Content="G" />
166
-
167
84
  <Slider
168
-
169
85
  Name="Sld10G"
170
-
171
86
  Grid.Column="1"
172
-
173
- VerticalAlignment="Center"
87
+ VerticalAlignment="Center"
174
-
175
88
  Maximum="255"
176
-
177
89
  Value="{Binding G}" />
178
-
179
90
  <TextBox
180
-
181
91
  Grid.Column="2"
182
-
183
92
  Width="50"
184
-
185
93
  Margin="5,0"
186
-
187
- VerticalAlignment="Center"
94
+ VerticalAlignment="Center"
188
-
189
95
  InputMethod.PreferredImeConversionMode="Alphanumeric"
190
-
191
96
  InputMethod.PreferredImeState="On"
192
-
193
97
  MaxLength="3"
194
-
195
98
  PreviewTextInput="TextBox_PreviewTextInput"
196
-
197
99
  Text="{Binding Value, ElementName=Sld10G, UpdateSourceTrigger=PropertyChanged}" />
198
-
199
100
  </Grid>
200
-
201
101
  <Grid>
202
-
203
102
  <Grid.ColumnDefinitions>
204
-
205
- <ColumnDefinition Width="Auto" />
103
+ <ColumnDefinition Width="Auto" />
206
-
207
104
  <ColumnDefinition />
208
-
209
- <ColumnDefinition Width="Auto" />
105
+ <ColumnDefinition Width="Auto" />
210
-
211
106
  </Grid.ColumnDefinitions>
212
-
213
107
  <Label VerticalAlignment="Center" Content="B" />
214
-
215
108
  <Slider
216
-
217
109
  Name="Sld10B"
218
-
219
110
  Grid.Column="1"
220
-
221
- VerticalAlignment="Center"
111
+ VerticalAlignment="Center"
222
-
223
112
  Maximum="255"
224
-
225
113
  Value="{Binding B}" />
226
-
227
114
  <TextBox
228
-
229
115
  Grid.Column="2"
230
-
231
116
  Width="50"
232
-
233
117
  Margin="5,0"
234
-
235
- VerticalAlignment="Center"
118
+ VerticalAlignment="Center"
236
-
237
119
  InputMethod.PreferredImeConversionMode="Alphanumeric"
238
-
239
120
  InputMethod.PreferredImeState="On"
240
-
241
121
  MaxLength="3"
242
-
243
122
  PreviewTextInput="TextBox_PreviewTextInput"
244
-
245
123
  Text="{Binding Value, ElementName=Sld10B, UpdateSourceTrigger=PropertyChanged}" />
246
-
247
124
  </Grid>
248
-
249
125
  </StackPanel>
250
-
251
126
  </Border>
252
-
253
127
  </DataTemplate>
254
-
255
128
  </ItemsControl.ItemTemplate>
256
-
257
129
  <!--
258
-
259
130
  C#コードから色を追加することになるでしょうが、何もないとデザインしずらいので仮に入れておく。
260
-
261
131
  完成したら削除すればよい。
262
-
263
132
  -->
264
-
265
133
  <Color>#E18F9B</Color>
266
-
267
134
  <Color>#C41A41</Color>
268
-
269
135
  <Color>#E2694A</Color>
270
-
271
136
  <Color>#90673E</Color>
272
-
273
137
  </ItemsControl>
274
-
275
138
  </Grid>
276
-
277
139
  </Window>
278
-
279
140
  ```
280
141
 
281
-
282
-
283
- ```C#
142
+ ```cs
284
-
285
143
  using System;
286
-
287
144
  using System.Diagnostics;
288
-
289
145
  using System.Globalization;
290
-
291
146
  using System.Linq;
292
-
293
147
  using System.Text.RegularExpressions;
294
-
295
148
  using System.Windows;
296
-
297
149
  using System.Windows.Controls;
298
-
299
150
  using System.Windows.Data;
300
-
301
151
  using System.Windows.Input;
302
-
303
152
  using System.Windows.Media;
304
153
 
305
-
306
-
307
154
  namespace Questions235362
308
-
309
155
  {
310
-
311
156
  public partial class MainWindow : Window
312
-
313
157
  {
314
-
315
158
  public MainWindow()
316
-
317
159
  {
318
-
319
160
  InitializeComponent();
320
-
321
161
  // コードから色を追加する場合(色のチョイスに特に意味はありません)
322
-
323
162
  //itemsControl.Items.Add(Color.FromRgb(255, 255, 255));
324
-
325
163
  itemsControl.Items.Add(ColorConverter.ConvertFromString("#EFA92A"));
326
-
327
164
  itemsControl.Items.Add(ColorConverter.ConvertFromString("#D68E31"));
328
-
329
165
  itemsControl.Items.Add(ColorConverter.ConvertFromString("#A0CA5A"));
330
-
331
166
  itemsControl.Items.Add(ColorConverter.ConvertFromString("#006D4D"));
332
-
333
167
  itemsControl.Items.Add(ColorConverter.ConvertFromString("#134A63"));
334
-
335
168
  itemsControl.Items.Add(ColorConverter.ConvertFromString("#009C9E"));
336
-
337
169
  itemsControl.Items.Add(ColorConverter.ConvertFromString("#B974A4"));
338
-
339
170
  itemsControl.Items.Add(ColorConverter.ConvertFromString("#634E86"));
340
-
341
171
  itemsControl.Items.Add(ColorConverter.ConvertFromString("#7B8075"));
342
-
343
172
  itemsControl.Items.Add(ColorConverter.ConvertFromString("#212222"));
344
-
345
173
  itemsControl.Items.Add(ColorConverter.ConvertFromString("#FFFDE6"));
346
-
347
174
  itemsControl.Items.Add(ColorConverter.ConvertFromString("#F6FBF6"));
348
-
349
175
  }
350
176
 
351
-
352
-
353
177
  private void BtnOk_Click(object sender, RoutedEventArgs e)
354
-
355
178
  {
356
-
357
179
  Debug.WriteLine(itemsControl.Items.Count); // 色の個数
358
-
359
180
  Debug.WriteLine((Color)itemsControl.Items[0]); // いっこめの色(左上)
360
181
 
361
-
362
-
363
182
  // パレット色の配列
364
-
365
183
  Color[] colors = itemsControl.Items.OfType<Color>().ToArray();
366
184
 
367
-
368
-
369
185
  var s = string.Join("\n", colors.Select(c => $"{c} R:{c.R} G:{c.G} B:{c.B}"));
370
-
371
186
  MessageBox.Show(s, "パレット一覧");
372
-
373
187
  }
374
188
 
375
-
376
-
377
189
  // TextBoxの入力制限
378
-
379
190
  private readonly Regex regex = new Regex("[0-9]+");
380
-
381
191
  private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
382
-
383
192
  {
384
-
385
193
  e.Handled = true;
386
-
387
194
  if(sender is TextBox textBox)
388
-
389
195
  {
390
-
391
196
  string text;
392
-
393
197
  if(0 < textBox.SelectedText.Length)
394
-
395
198
  text = textBox.Text.Replace(textBox.SelectedText, e.Text);
396
-
397
199
  else
398
-
399
200
  text = textBox.Text.Insert(textBox.CaretIndex, e.Text);
400
201
 
401
-
402
-
403
202
  if(3 < text.Length) return;
404
-
405
203
  if(!regex.IsMatch(text)) return;
406
-
407
204
  if(!int.TryParse(text, out var i)) return;
408
-
409
205
  if(255 < i) return;
410
206
 
411
-
412
-
413
207
  e.Handled = false;
414
-
415
208
  }
416
-
417
209
  }
418
-
419
210
  }
420
211
 
421
-
422
-
423
212
  // RGB値からBrushに変換
424
-
425
213
  internal class BrushConverter : IMultiValueConverter
426
-
427
214
  {
428
-
429
215
  public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
430
-
431
216
  => new SolidColorBrush(Color.FromRgb(System.Convert.ToByte(values[0]), System.Convert.ToByte(values[1]), System.Convert.ToByte(values[2])));
432
-
433
217
  public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
434
-
435
218
  => throw new NotSupportedException();
436
-
437
219
  }
438
-
439
220
  }
440
-
441
221
  ```
442
222
 
443
-
444
-
445
- TextBoxの入力制限が雑です(クリップボードからの貼り付けができてしまう)
223
+ `TextBox`の入力制限が雑です(クリップボードからの貼り付けができてしまう)
446
-
447
224
  [IntegerUpDown · xceedsoftware/wpftoolkit Wiki](https://github.com/xceedsoftware/wpftoolkit/wiki/IntegerUpDown) こういったものを使ったほうが楽です。
448
225
 
449
-
450
-
451
226
  XAMLではバインディングやテンプレート等非常に強力な機能がありますので、ゆっくりでも勉強していただけたらと思います。
452
227
 
453
228
 
454
-
455
-
456
-
457
229
  [ItemsControl 攻略 ~ 外観のカスタマイズ | grabacr.nét](http://grabacr.net/archives/1240)
458
230
 
459
-
460
-
461
231
  [WPF4.5入門](https://www.slideshare.net/okazuki0130/wpf45-38048141)
462
-
463
232
  [WPF 4.5入門 | 大田一希 | 工学 | Kindleストア | Amazon](https://www.amazon.co.jp/WPF-4-5%E5%85%A5%E9%96%80-%E5%A4%A7%E7%94%B0%E4%B8%80%E5%B8%8C-ebook/dp/B015WXYUUU/ref=sr_1_4?qid=1579158573&s=digital-text&sr=1-4&text=%E5%A4%A7%E7%94%B0%E4%B8%80%E5%B8%8C)
464
-
465
233
  内容は同じだと思います。
466
234
 
467
-
468
-
469
235
  [ » WPF 学習用ドキュメント作りました](http://kisuke0303.sakura.ne.jp/blog/?p=340)