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

回答編集履歴

1

見直しキャンペーン中

2023/07/27 15:39

投稿

TN8001
TN8001

スコア10104

answer CHANGED
@@ -1,171 +1,171 @@
1
- 解決されているので蛇足ですが、文字列から`Brush`への変換は`BrushConverter`が用意されています。
2
- [BrushConverter クラス (System.Windows.Media) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.media.brushconverter)
3
-
4
- 表示される文字列を日本語にしたり色見本を出したくなったときに、dodox86さんの回答のように辞書等で持っておくと楽なので私もこうしますね。
5
-
6
- ```xaml
7
- <Window
8
- x:Class="Questions344646.MainWindow"
9
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
10
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
11
- xmlns:System="clr-namespace:System;assembly=mscorlib"
12
- Width="600"
13
- Height="400"
14
- Background="LightGray">
15
- <Window.Resources>
16
- <x:Array x:Key="Colors" Type="System:String">
17
- <System:String>Red</System:String>
18
- <System:String>Blue</System:String>
19
- <System:String>Green</System:String>
20
- <System:String>White</System:String>
21
- <System:String>Black</System:String>
22
- <System:String>NoColor</System:String>
23
- </x:Array>
24
- </Window.Resources>
25
- <Grid>
26
- <Grid.ColumnDefinitions>
27
- <ColumnDefinition />
28
- <ColumnDefinition />
29
- </Grid.ColumnDefinitions>
30
-
31
- <GroupBox Header="BrushConverter">
32
- <Grid>
33
- <Grid.RowDefinitions>
34
- <RowDefinition Height="Auto" />
35
- <RowDefinition />
36
- </Grid.RowDefinitions>
37
- <DockPanel>
38
- <TextBlock VerticalAlignment="Center" Text="線の色" />
39
- <ComboBox
40
- x:Name="BordColorC"
41
- Margin="5"
42
- ItemsSource="{StaticResource Colors}"
43
- SelectedValue="{Binding DefoBordColor}"
44
- SelectionChanged="BordColorC_SelectionChanged" />
45
- </DockPanel>
46
- <Rectangle
47
- Name="Rectan"
48
- Grid.Row="1"
49
- Width="120"
50
- Height="100"
51
- Stroke="Black"
52
- StrokeThickness="2" />
53
- </Grid>
54
- </GroupBox>
55
-
56
- <GroupBox Grid.Column="1" Header="Dictionary">
57
- <Grid>
58
- <Grid.RowDefinitions>
59
- <RowDefinition Height="Auto" />
60
- <RowDefinition />
61
- </Grid.RowDefinitions>
62
- <DockPanel>
63
- <TextBlock VerticalAlignment="Center" Text="線の色" />
64
- <ComboBox
65
- x:Name="BordColorC2"
66
- Margin="5"
67
- ItemsSource="{Binding Colors}"
68
- SelectedValue="{Binding DefoBordColor2}"
69
- SelectedValuePath="Key"
70
- SelectionChanged="BordColorC2_SelectionChanged">
71
- <ComboBox.ItemTemplate>
72
- <DataTemplate>
73
- <DockPanel>
74
- <Ellipse
75
- Width="16"
76
- Height="16"
77
- Fill="{Binding Value}" />
78
- <TextBlock
79
- Margin="5,0"
80
- VerticalAlignment="Center"
81
- Text="{Binding Key}" />
82
- </DockPanel>
83
- </DataTemplate>
84
- </ComboBox.ItemTemplate>
85
- </ComboBox>
86
- </DockPanel>
87
- <Rectangle
88
- Name="Rectan2"
89
- Grid.Row="1"
90
- Width="120"
91
- Height="100"
92
- Stroke="Black"
93
- StrokeThickness="2" />
94
- </Grid>
95
- </GroupBox>
96
- </Grid>
97
- </Window>
98
- ```
99
-
100
- ```C#
101
- using System.Collections.Generic;
102
- using System.ComponentModel;
103
- using System.Windows;
104
- using System.Windows.Controls;
105
- using System.Windows.Media;
106
-
107
- namespace Questions344646
108
- {
109
- public class DefaultData
110
- {
111
- public static string DefoBordColor
112
- {
113
- get => Properties.Settings.Default.DefaBordColor;
114
- set => Properties.Settings.Default.DefaBordColor = value;
115
- }
116
- public static string DefoBordColor2
117
- {
118
- get => Properties.Settings.Default.DefaBordColor2;
119
- set => Properties.Settings.Default.DefaBordColor2 = value;
120
- }
121
-
122
- public static Dictionary<string, SolidColorBrush> Colors { get; } =
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
- }
133
-
134
- public partial class MainWindow : Window
135
- {
136
- public MainWindow()
137
- {
138
- InitializeComponent();
139
-
140
- // xamlから逆算するとこうなっているってこと??(ちょっと変わってますね^^;
141
- DataContext = new DefaultData();
142
- }
143
-
144
- protected override void OnClosing(CancelEventArgs e)
145
- {
146
- Properties.Settings.Default.Save();
147
- }
148
-
149
- private void BordColorC_SelectionChanged(object sender, SelectionChangedEventArgs e)
150
- {
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
- {
159
- Rectan.Stroke = Brushes.Transparent;
160
- }
161
- }
162
-
163
- private void BordColorC2_SelectionChanged(object sender, SelectionChangedEventArgs e)
164
- {
165
- var pair = (KeyValuePair<string, SolidColorBrush>)BordColorC2.SelectedItem;
166
- Rectan2.Stroke = pair.Value;
167
- }
168
- }
169
- }
170
- ```
1
+ 解決されているので蛇足ですが、文字列から`Brush`への変換は`BrushConverter`が用意されています。
2
+ [BrushConverter クラス (System.Windows.Media) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.media.brushconverter)
3
+
4
+ 表示される文字列を日本語にしたり色見本を出したくなったときに、dodox86さんの回答のように辞書等で持っておくと楽なので私もこうしますね。
5
+
6
+ ```xml
7
+ <Window
8
+ x:Class="Questions344646.MainWindow"
9
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
10
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
11
+ xmlns:System="clr-namespace:System;assembly=mscorlib"
12
+ Width="600"
13
+ Height="400"
14
+ Background="LightGray">
15
+ <Window.Resources>
16
+ <x:Array x:Key="Colors" Type="System:String">
17
+ <System:String>Red</System:String>
18
+ <System:String>Blue</System:String>
19
+ <System:String>Green</System:String>
20
+ <System:String>White</System:String>
21
+ <System:String>Black</System:String>
22
+ <System:String>NoColor</System:String>
23
+ </x:Array>
24
+ </Window.Resources>
25
+ <Grid>
26
+ <Grid.ColumnDefinitions>
27
+ <ColumnDefinition />
28
+ <ColumnDefinition />
29
+ </Grid.ColumnDefinitions>
30
+
31
+ <GroupBox Header="BrushConverter">
32
+ <Grid>
33
+ <Grid.RowDefinitions>
34
+ <RowDefinition Height="Auto" />
35
+ <RowDefinition />
36
+ </Grid.RowDefinitions>
37
+ <DockPanel>
38
+ <TextBlock VerticalAlignment="Center" Text="線の色" />
39
+ <ComboBox
40
+ x:Name="BordColorC"
41
+ Margin="5"
42
+ ItemsSource="{StaticResource Colors}"
43
+ SelectedValue="{Binding DefoBordColor}"
44
+ SelectionChanged="BordColorC_SelectionChanged" />
45
+ </DockPanel>
46
+ <Rectangle
47
+ Name="Rectan"
48
+ Grid.Row="1"
49
+ Width="120"
50
+ Height="100"
51
+ Stroke="Black"
52
+ StrokeThickness="2" />
53
+ </Grid>
54
+ </GroupBox>
55
+
56
+ <GroupBox Grid.Column="1" Header="Dictionary">
57
+ <Grid>
58
+ <Grid.RowDefinitions>
59
+ <RowDefinition Height="Auto" />
60
+ <RowDefinition />
61
+ </Grid.RowDefinitions>
62
+ <DockPanel>
63
+ <TextBlock VerticalAlignment="Center" Text="線の色" />
64
+ <ComboBox
65
+ x:Name="BordColorC2"
66
+ Margin="5"
67
+ ItemsSource="{Binding Colors}"
68
+ SelectedValue="{Binding DefoBordColor2}"
69
+ SelectedValuePath="Key"
70
+ SelectionChanged="BordColorC2_SelectionChanged">
71
+ <ComboBox.ItemTemplate>
72
+ <DataTemplate>
73
+ <DockPanel>
74
+ <Ellipse
75
+ Width="16"
76
+ Height="16"
77
+ Fill="{Binding Value}" />
78
+ <TextBlock
79
+ Margin="5,0"
80
+ VerticalAlignment="Center"
81
+ Text="{Binding Key}" />
82
+ </DockPanel>
83
+ </DataTemplate>
84
+ </ComboBox.ItemTemplate>
85
+ </ComboBox>
86
+ </DockPanel>
87
+ <Rectangle
88
+ Name="Rectan2"
89
+ Grid.Row="1"
90
+ Width="120"
91
+ Height="100"
92
+ Stroke="Black"
93
+ StrokeThickness="2" />
94
+ </Grid>
95
+ </GroupBox>
96
+ </Grid>
97
+ </Window>
98
+ ```
99
+
100
+ ```cs
101
+ using System.Collections.Generic;
102
+ using System.ComponentModel;
103
+ using System.Windows;
104
+ using System.Windows.Controls;
105
+ using System.Windows.Media;
106
+
107
+ namespace Questions344646
108
+ {
109
+ public class DefaultData
110
+ {
111
+ public static string DefoBordColor
112
+ {
113
+ get => Properties.Settings.Default.DefaBordColor;
114
+ set => Properties.Settings.Default.DefaBordColor = value;
115
+ }
116
+ public static string DefoBordColor2
117
+ {
118
+ get => Properties.Settings.Default.DefaBordColor2;
119
+ set => Properties.Settings.Default.DefaBordColor2 = value;
120
+ }
121
+
122
+ public static Dictionary<string, SolidColorBrush> Colors { get; } =
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
+ }
133
+
134
+ public partial class MainWindow : Window
135
+ {
136
+ public MainWindow()
137
+ {
138
+ InitializeComponent();
139
+
140
+ // xamlから逆算するとこうなっているってこと??(ちょっと変わってますね^^;
141
+ DataContext = new DefaultData();
142
+ }
143
+
144
+ protected override void OnClosing(CancelEventArgs e)
145
+ {
146
+ Properties.Settings.Default.Save();
147
+ }
148
+
149
+ private void BordColorC_SelectionChanged(object sender, SelectionChangedEventArgs e)
150
+ {
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
+ {
159
+ Rectan.Stroke = Brushes.Transparent;
160
+ }
161
+ }
162
+
163
+ private void BordColorC2_SelectionChanged(object sender, SelectionChangedEventArgs e)
164
+ {
165
+ var pair = (KeyValuePair<string, SolidColorBrush>)BordColorC2.SelectedItem;
166
+ Rectan2.Stroke = pair.Value;
167
+ }
168
+ }
169
+ }
170
+ ```
171
171
  ![アプリ画像](ce0166af3c06898fd2f0b7fa5794fb5f.png)