回答編集履歴

1

見直しキャンペーン中

2023/07/26 13:15

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,247 +1,124 @@
1
1
  あまり自信がありませんが、こういうことでしょうか?
2
2
 
3
-
4
-
5
- ```xaml
3
+ ```xml
6
-
7
4
  <Window
8
-
9
5
  x:Class="Questions316881.MainWindow"
10
-
11
6
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
12
-
13
7
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
14
-
15
8
  xmlns:behaviors="http://schemas.microsoft.com/xaml/behaviors"
16
-
17
9
  Width="800"
18
-
19
10
  Height="450">
20
-
21
11
  <Window.Resources>
22
-
23
12
  <!--<ControlTemplate x:Key="ButtonTemplate" TargetType="Button">
24
-
25
13
  <Grid>
26
-
27
14
  <StackPanel>
28
-
29
15
  <Ellipse Height="100">
30
-
31
16
  <Ellipse.Resources>
32
-
33
17
  <Style TargetType="Ellipse">
34
-
35
18
  <Setter Property="Fill" Value="Gray" />
36
-
37
19
  <Style.Triggers>
38
-
39
20
  <DataTrigger Binding="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}" Value="赤丸">
40
-
41
21
  <Setter Property="Fill" Value="Red" />
42
-
43
22
  </DataTrigger>
44
-
45
23
  <DataTrigger Binding="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}" Value="緑丸">
46
-
47
24
  <Setter Property="Fill" Value="Green" />
48
-
49
25
  </DataTrigger>
50
-
51
26
  <DataTrigger Binding="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}" Value="青丸">
52
-
53
27
  <Setter Property="Fill" Value="Blue" />
54
-
55
28
  </DataTrigger>
56
-
57
29
  </Style.Triggers>
58
-
59
30
  </Style>
60
-
61
31
  </Ellipse.Resources>
62
-
63
32
  </Ellipse>
64
-
65
33
  <Rectangle Height="100" Fill="Gray" />
66
-
67
34
  <Label Content="{TemplateBinding Tag}" />
68
-
69
35
  </StackPanel>
70
-
71
36
  <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
72
-
73
37
  </Grid>
74
-
75
38
  </ControlTemplate>-->
76
-
77
-
78
39
 
79
40
  <!-- ↑これでも動きますが、↓の方が少し短いしわかりやすそうです -->
80
41
 
81
-
82
-
83
42
  <ControlTemplate x:Key="ButtonTemplate" TargetType="Button">
84
-
85
43
  <Grid>
86
-
87
44
  <StackPanel>
88
-
89
45
  <Ellipse
90
-
91
46
  x:Name="ellipse"
92
-
93
47
  Height="100"
94
-
95
48
  Fill="Gray" />
96
-
97
49
  <Rectangle Height="100" Fill="Gray" />
98
-
99
50
  <Label Content="{TemplateBinding Tag}" />
100
-
101
51
  </StackPanel>
102
-
103
52
  <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
104
-
105
53
  </Grid>
106
-
107
54
  <ControlTemplate.Triggers>
108
-
109
55
  <Trigger Property="Tag" Value="赤丸">
110
-
111
56
  <Setter TargetName="ellipse" Property="Fill" Value="Red" />
112
-
113
57
  </Trigger>
114
-
115
58
  <Trigger Property="Tag" Value="緑丸">
116
-
117
59
  <Setter TargetName="ellipse" Property="Fill" Value="Green" />
118
-
119
60
  </Trigger>
120
-
121
61
  <Trigger Property="Tag" Value="青丸">
122
-
123
62
  <Setter TargetName="ellipse" Property="Fill" Value="Blue" />
124
-
125
63
  </Trigger>
126
-
127
64
  </ControlTemplate.Triggers>
128
-
129
65
  </ControlTemplate>
130
-
131
66
  </Window.Resources>
132
-
133
67
  <StackPanel
134
-
135
68
  HorizontalAlignment="Center"
136
-
137
69
  VerticalAlignment="Center"
138
-
139
70
  Orientation="Horizontal">
140
-
141
71
 
72
+ <Button
73
+ Content="ButtonA"
74
+ Tag="{Binding ButtonAFlag}"
75
+ Template="{StaticResource ButtonTemplate}">
76
+ <behaviors:Interaction.Triggers>
77
+ <behaviors:EventTrigger EventName="PreviewMouseLeftButtonUp">
78
+ <behaviors:CallMethodAction MethodName="ButtonAOn" TargetObject="{Binding}" />
79
+ </behaviors:EventTrigger>
80
+ </behaviors:Interaction.Triggers>
81
+ </Button>
142
82
 
143
83
  <Button
144
-
145
- Content="ButtonA"
84
+ Content="ButtonB"
146
-
147
- Tag="{Binding ButtonAFlag}"
85
+ Tag="{Binding ButtonBFlag}"
148
-
149
86
  Template="{StaticResource ButtonTemplate}">
150
-
151
87
  <behaviors:Interaction.Triggers>
152
-
153
88
  <behaviors:EventTrigger EventName="PreviewMouseLeftButtonUp">
154
-
155
- <behaviors:CallMethodAction MethodName="ButtonAOn" TargetObject="{Binding}" />
89
+ <behaviors:CallMethodAction MethodName="ButtonBOn" TargetObject="{Binding}" />
156
-
157
90
  </behaviors:EventTrigger>
158
-
159
91
  </behaviors:Interaction.Triggers>
160
-
161
92
  </Button>
162
-
163
-
164
-
165
- <Button
166
-
167
- Content="ButtonB"
168
-
169
- Tag="{Binding ButtonBFlag}"
170
-
171
- Template="{StaticResource ButtonTemplate}">
172
-
173
- <behaviors:Interaction.Triggers>
174
-
175
- <behaviors:EventTrigger EventName="PreviewMouseLeftButtonUp">
176
-
177
- <behaviors:CallMethodAction MethodName="ButtonBOn" TargetObject="{Binding}" />
178
-
179
- </behaviors:EventTrigger>
180
-
181
- </behaviors:Interaction.Triggers>
182
-
183
- </Button>
184
-
185
93
  </StackPanel>
186
-
187
94
  </Window>
188
-
189
95
  ```
190
96
 
191
-
192
-
193
- ```C#
97
+ ```cs
194
-
195
98
  using Livet;
196
-
197
99
  using System.Windows;
198
100
 
199
-
200
-
201
101
  namespace Questions316881
202
-
203
102
  {
204
-
205
103
  public class MainViewModel : ViewModel
206
-
207
104
  {
208
-
209
105
  private string _ButtonAFlag = "赤丸";
210
-
211
106
  public string ButtonAFlag { get => _ButtonAFlag; set => RaisePropertyChangedIfSet(ref _ButtonAFlag, value); }
212
107
 
213
-
214
-
215
108
  private string _ButtonBFlag = "青丸";
216
-
217
109
  public string ButtonBFlag { get => _ButtonBFlag; set => RaisePropertyChangedIfSet(ref _ButtonBFlag, value); }
218
110
 
219
-
220
-
221
111
  public void ButtonAOn() => ButtonAFlag = ButtonAFlag == "赤丸" ? "緑丸" : "赤丸";
222
-
223
112
  public void ButtonBOn() => ButtonBFlag = ButtonBFlag == "青丸" ? "緑丸" : "青丸";
224
-
225
113
  }
226
114
 
227
-
228
-
229
115
  public partial class MainWindow : Window
230
-
231
116
  {
232
-
233
117
  public MainWindow()
234
-
235
118
  {
236
-
237
119
  InitializeComponent();
238
-
239
120
  DataContext = new MainViewModel();
240
-
241
121
  }
242
-
243
122
  }
244
-
245
123
  }
246
-
247
124
  ```