回答編集履歴

1

見直しキャンペーン中

2023/07/27 15:39

投稿

TN8001
TN8001

スコア9813

test CHANGED
@@ -1,341 +1,171 @@
1
1
  解決されているので蛇足ですが、文字列から`Brush`への変換は`BrushConverter`が用意されています。
2
-
3
2
  [BrushConverter クラス (System.Windows.Media) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.media.brushconverter)
4
-
5
-
6
3
 
7
4
  表示される文字列を日本語にしたり色見本を出したくなったときに、dodox86さんの回答のように辞書等で持っておくと楽なので私もこうしますね。
8
5
 
9
-
10
-
11
- ```xaml
6
+ ```xml
12
-
13
7
  <Window
14
-
15
8
  x:Class="Questions344646.MainWindow"
16
-
17
9
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
18
-
19
10
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
20
-
21
11
  xmlns:System="clr-namespace:System;assembly=mscorlib"
22
-
23
12
  Width="600"
24
-
25
13
  Height="400"
26
-
27
14
  Background="LightGray">
28
-
29
15
  <Window.Resources>
30
-
31
16
  <x:Array x:Key="Colors" Type="System:String">
32
-
33
17
  <System:String>Red</System:String>
34
-
35
18
  <System:String>Blue</System:String>
36
-
37
19
  <System:String>Green</System:String>
38
-
39
20
  <System:String>White</System:String>
40
-
41
21
  <System:String>Black</System:String>
42
-
43
22
  <System:String>NoColor</System:String>
44
-
45
23
  </x:Array>
46
-
47
24
  </Window.Resources>
48
-
49
25
  <Grid>
50
-
51
26
  <Grid.ColumnDefinitions>
52
-
53
27
  <ColumnDefinition />
54
-
55
28
  <ColumnDefinition />
56
-
57
29
  </Grid.ColumnDefinitions>
58
30
 
59
-
60
-
61
31
  <GroupBox Header="BrushConverter">
62
-
63
32
  <Grid>
64
-
65
33
  <Grid.RowDefinitions>
66
-
67
34
  <RowDefinition Height="Auto" />
68
-
69
35
  <RowDefinition />
70
-
71
36
  </Grid.RowDefinitions>
72
-
73
37
  <DockPanel>
74
-
75
38
  <TextBlock VerticalAlignment="Center" Text="線の色" />
76
-
77
39
  <ComboBox
78
-
79
40
  x:Name="BordColorC"
80
-
81
41
  Margin="5"
82
-
83
42
  ItemsSource="{StaticResource Colors}"
84
-
85
43
  SelectedValue="{Binding DefoBordColor}"
86
-
87
44
  SelectionChanged="BordColorC_SelectionChanged" />
88
-
89
45
  </DockPanel>
90
-
91
46
  <Rectangle
92
-
93
47
  Name="Rectan"
94
-
95
48
  Grid.Row="1"
96
-
97
49
  Width="120"
98
-
99
50
  Height="100"
100
-
101
51
  Stroke="Black"
102
-
103
52
  StrokeThickness="2" />
104
-
105
53
  </Grid>
106
-
107
54
  </GroupBox>
108
55
 
109
-
110
-
111
56
  <GroupBox Grid.Column="1" Header="Dictionary">
112
-
113
57
  <Grid>
114
-
115
58
  <Grid.RowDefinitions>
116
-
117
59
  <RowDefinition Height="Auto" />
118
-
119
60
  <RowDefinition />
120
-
121
61
  </Grid.RowDefinitions>
122
-
123
62
  <DockPanel>
124
-
125
63
  <TextBlock VerticalAlignment="Center" Text="線の色" />
126
-
127
64
  <ComboBox
128
-
129
65
  x:Name="BordColorC2"
130
-
131
66
  Margin="5"
132
-
133
67
  ItemsSource="{Binding Colors}"
134
-
135
68
  SelectedValue="{Binding DefoBordColor2}"
136
-
137
69
  SelectedValuePath="Key"
138
-
139
70
  SelectionChanged="BordColorC2_SelectionChanged">
140
-
141
71
  <ComboBox.ItemTemplate>
142
-
143
72
  <DataTemplate>
144
-
145
73
  <DockPanel>
146
-
147
74
  <Ellipse
148
-
149
75
  Width="16"
150
-
151
76
  Height="16"
152
-
153
77
  Fill="{Binding Value}" />
154
-
155
78
  <TextBlock
156
-
157
79
  Margin="5,0"
158
-
159
80
  VerticalAlignment="Center"
160
-
161
81
  Text="{Binding Key}" />
162
-
163
82
  </DockPanel>
164
-
165
83
  </DataTemplate>
166
-
167
84
  </ComboBox.ItemTemplate>
168
-
169
85
  </ComboBox>
170
-
171
86
  </DockPanel>
172
-
173
87
  <Rectangle
174
-
175
88
  Name="Rectan2"
176
-
177
89
  Grid.Row="1"
178
-
179
90
  Width="120"
180
-
181
91
  Height="100"
182
-
183
92
  Stroke="Black"
184
-
185
93
  StrokeThickness="2" />
186
-
187
94
  </Grid>
188
-
189
95
  </GroupBox>
190
-
191
96
  </Grid>
192
-
193
97
  </Window>
194
-
195
98
  ```
196
99
 
197
-
198
-
199
- ```C#
100
+ ```cs
200
-
201
101
  using System.Collections.Generic;
202
-
203
102
  using System.ComponentModel;
204
-
205
103
  using System.Windows;
206
-
207
104
  using System.Windows.Controls;
208
-
209
105
  using System.Windows.Media;
210
106
 
211
-
212
-
213
107
  namespace Questions344646
214
-
215
108
  {
216
-
217
109
  public class DefaultData
218
-
219
110
  {
220
-
221
111
  public static string DefoBordColor
222
-
223
112
  {
224
-
225
113
  get => Properties.Settings.Default.DefaBordColor;
226
-
227
114
  set => Properties.Settings.Default.DefaBordColor = value;
228
-
115
+ }
116
+ public static string DefoBordColor2
117
+ {
118
+ get => Properties.Settings.Default.DefaBordColor2;
119
+ set => Properties.Settings.Default.DefaBordColor2 = value;
229
120
  }
230
121
 
122
+ public static Dictionary<string, SolidColorBrush> Colors { get; } =
231
- public static string DefoBordColor2
123
+ new Dictionary<string, SolidColorBrush>
124
+ {
125
+ {"Red", Brushes.Red },
126
+ {"Blue", Brushes.Blue },
127
+ {"Green", Brushes.Green },
128
+ {"白", Brushes.White },
129
+ {"黒", Brushes.Black },
130
+ {"透明", Brushes.Transparent },
131
+ };
132
+ }
232
133
 
134
+ public partial class MainWindow : Window
135
+ {
136
+ public MainWindow()
233
137
  {
138
+ InitializeComponent();
234
139
 
140
+ // xamlから逆算するとこうなっているってこと??(ちょっと変わってますね^^;
235
- get => Properties.Settings.Default.DefaBordColor2;
141
+ DataContext = new DefaultData();
236
-
237
- set => Properties.Settings.Default.DefaBordColor2 = value;
238
-
239
142
  }
240
143
 
241
-
242
-
243
- public static Dictionary<string, SolidColorBrush> Colors { get; } =
244
-
245
- new Dictionary<string, SolidColorBrush>
144
+ protected override void OnClosing(CancelEventArgs e)
246
-
247
- {
248
-
249
- {"Red", Brushes.Red },
250
-
251
- {"Blue", Brushes.Blue },
252
-
253
- {"Green", Brushes.Green },
254
-
255
- {"白", Brushes.White },
256
-
257
- {"黒", Brushes.Black },
258
-
259
- {"透明", Brushes.Transparent },
260
-
261
- };
262
-
263
- }
264
-
265
-
266
-
267
- public partial class MainWindow : Window
268
-
269
- {
270
-
271
- public MainWindow()
272
-
273
145
  {
274
-
275
- InitializeComponent();
276
-
277
-
278
-
279
- // xamlから逆算するとこうなっているってこと??(ちょっと変わってますね^^;
280
-
281
- DataContext = new DefaultData();
146
+ Properties.Settings.Default.Save();
282
-
283
147
  }
284
148
 
285
-
286
-
287
- protected override void OnClosing(CancelEventArgs e)
149
+ private void BordColorC_SelectionChanged(object sender, SelectionChangedEventArgs e)
288
-
289
150
  {
290
-
151
+ var name = BordColorC.SelectedItem.ToString();
152
+ try
153
+ {
154
+ var brush = (SolidColorBrush)new BrushConverter().ConvertFromString(name);
155
+ Rectan.Stroke = brush;
156
+ }
157
+ catch
158
+ {
291
- Properties.Settings.Default.Save();
159
+ Rectan.Stroke = Brushes.Transparent;
292
-
160
+ }
293
161
  }
294
162
 
295
-
296
-
297
- private void BordColorC_SelectionChanged(object sender, SelectionChangedEventArgs e)
163
+ private void BordColorC2_SelectionChanged(object sender, SelectionChangedEventArgs e)
298
-
299
164
  {
300
-
301
- var name = BordColorC.SelectedItem.ToString();
302
-
303
- try
304
-
305
- {
306
-
307
- var brush = (SolidColorBrush)new BrushConverter().ConvertFromString(name);
165
+ var pair = (KeyValuePair<string, SolidColorBrush>)BordColorC2.SelectedItem;
308
-
309
- Rectan.Stroke = brush;
166
+ Rectan2.Stroke = pair.Value;
310
-
311
- }
312
-
313
- catch
314
-
315
- {
316
-
317
- Rectan.Stroke = Brushes.Transparent;
318
-
319
- }
320
-
321
167
  }
322
-
323
-
324
-
325
- private void BordColorC2_SelectionChanged(object sender, SelectionChangedEventArgs e)
326
-
327
- {
328
-
329
- var pair = (KeyValuePair<string, SolidColorBrush>)BordColorC2.SelectedItem;
330
-
331
- Rectan2.Stroke = pair.Value;
332
-
333
- }
334
-
335
168
  }
336
-
337
169
  }
338
-
339
170
  ```
340
-
341
171
  ![アプリ画像](ce0166af3c06898fd2f0b7fa5794fb5f.png)