質問編集履歴

1

サンプルを作成しましたので、ViewModelを含めたソースを記載します。

2019/04/15 03:00

投稿

inkan
inkan

スコア13

test CHANGED
File without changes
test CHANGED
@@ -22,12 +22,256 @@
22
22
 
23
23
  ### 該当のソース
24
24
 
25
+ ※サンプルを作成しましたので、ViewModelを含めたソースを記載します。
26
+
27
+
28
+
29
+ ■View
30
+
31
+ ```ここに言語を入力
32
+
33
+ <Window x:Class="MySoundPlayer.MainWindow"
34
+
35
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
36
+
37
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
38
+
39
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
40
+
41
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
42
+
43
+ xmlns:vm="clr-namespace:MySoundPlayer.ViewModel"
44
+
45
+ mc:Ignorable="d"
46
+
47
+ Title="MainWindow" Height="200" Width="200">
48
+
49
+
50
+
51
+ <Window.DataContext>
52
+
53
+ <vm:SoundPlayerViewModel />
54
+
55
+ </Window.DataContext>
56
+
57
+ <Window.Resources>
58
+
59
+ <!--System Button-->
60
+
61
+ <Style x:Key="SoundButtonStyle" TargetType="{x:Type ToggleButton}">
62
+
63
+ <Setter Property="Template">
64
+
65
+ <Setter.Value>
66
+
67
+ <ControlTemplate x:Uid="SoundButtonTemplate" TargetType="{x:Type ToggleButton}">
68
+
69
+ <Grid x:Name="ButtonImageBrush" Opacity="1" Background="LightGray">
70
+
71
+ <Label Name="Caption"
72
+
73
+ FontSize="16"
74
+
75
+ Content="{Binding ButtonLabel}"
76
+
77
+ VerticalContentAlignment="Center"
78
+
79
+ HorizontalContentAlignment="Center"/>
80
+
81
+ </Grid>
82
+
83
+ <ControlTemplate.Triggers>
84
+
85
+ <!--<EventTrigger RoutedEvent="Click">
86
+
87
+ <SoundPlayerAction Source="/Sounds\button.wav" />
88
+
89
+ </EventTrigger>-->
90
+
25
- <EventTrigger RoutedEvent="UIElement.PreviewMouseDown">
91
+ <EventTrigger RoutedEvent="Click">
26
-
27
- <EventTrigger.Actions>
92
+
28
-
29
- <SoundPlayerAction Source="{Binding SoundSource}" />
93
+ <SoundPlayerAction Source="{Binding SoundSource}" />
30
-
31
- </EventTrigger.Actions>
94
+
32
-
33
- </EventTrigger>
95
+ </EventTrigger>
96
+
97
+ <Trigger Property="IsFocused" Value="True"/>
98
+
99
+ <Trigger Property="IsMouseOver" Value="True"/>
100
+
101
+ <Trigger Property="IsPressed" Value="True">
102
+
103
+ <Setter Property="Background" TargetName="ButtonImageBrush">
104
+
105
+ <Setter.Value>
106
+
107
+ <SolidColorBrush Color="Gray" />
108
+
109
+ </Setter.Value>
110
+
111
+ </Setter>
112
+
113
+ </Trigger>
114
+
115
+ <Trigger Property="IsChecked" Value="True">
116
+
117
+ <Setter Property="Background" TargetName="ButtonImageBrush">
118
+
119
+ <Setter.Value>
120
+
121
+ <SolidColorBrush Color="LightCyan" />
122
+
123
+ </Setter.Value>
124
+
125
+ </Setter>
126
+
127
+ </Trigger>
128
+
129
+ </ControlTemplate.Triggers>
130
+
131
+ </ControlTemplate>
132
+
133
+ </Setter.Value>
134
+
135
+ </Setter>
136
+
137
+ </Style>
138
+
139
+
140
+
141
+ </Window.Resources>
142
+
143
+ <Grid>
144
+
145
+ <ToggleButton Name="BTN_System"
146
+
147
+ Width="50"
148
+
149
+ Height="50"
150
+
151
+ Style="{StaticResource SoundButtonStyle}" />
152
+
153
+ </Grid>
154
+
155
+ </Window>
156
+
157
+ ```
158
+
159
+ 上記のコメント部分を有効にすることで、Bindingしなければ音は鳴らせることは確認しました。
160
+
161
+
162
+
163
+
164
+
165
+ ■ViewModel
166
+
167
+ ```ここに言語を入力
168
+
169
+ using System;
170
+
171
+ using System.ComponentModel;
172
+
173
+
174
+
175
+ namespace MySoundPlayer.ViewModel
176
+
177
+ {
178
+
179
+ class SoundPlayerViewModel : INotifyPropertyChanged
180
+
181
+ {
182
+
183
+
184
+
185
+ private Uri _SoundSource;
186
+
187
+ public Uri SoundSource
188
+
189
+ {
190
+
191
+ get
192
+
193
+ {
194
+
195
+ return _SoundSource;
196
+
197
+ }
198
+
199
+ set
200
+
201
+ {
202
+
203
+ _SoundSource = value;
204
+
205
+ if (PropertyChanged != null)
206
+
207
+ {
208
+
209
+ PropertyChanged(this, new PropertyChangedEventArgs("SoundSource"));
210
+
211
+
212
+
213
+ }
214
+
215
+ }
216
+
217
+ }
218
+
219
+
220
+
221
+ private String _ButtonLabel;
222
+
223
+ public String ButtonLabel
224
+
225
+ {
226
+
227
+ get
228
+
229
+ {
230
+
231
+ return _ButtonLabel;
232
+
233
+ }
234
+
235
+ set
236
+
237
+ {
238
+
239
+ _ButtonLabel = value;
240
+
241
+ if (PropertyChanged != null)
242
+
243
+ {
244
+
245
+ PropertyChanged(this, new PropertyChangedEventArgs("ButtonLabel"));
246
+
247
+
248
+
249
+ }
250
+
251
+ }
252
+
253
+ }
254
+
255
+
256
+
257
+ public event PropertyChangedEventHandler PropertyChanged;
258
+
259
+
260
+
261
+ public SoundPlayerViewModel()
262
+
263
+ {
264
+
265
+ SoundSource = new Uri(@"/Sounds\button.wav", UriKind.Relative);
266
+
267
+ ButtonLabel = "Play!";
268
+
269
+ }
270
+
271
+ }
272
+
273
+ }
274
+
275
+
276
+
277
+ ```