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

質問編集履歴

1

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

2019/04/15 03:00

投稿

inkan
inkan

スコア13

title CHANGED
File without changes
body CHANGED
@@ -10,8 +10,130 @@
10
10
  UriクラスのSoundSourceプロパティをViewModelに定義し、バインディングしているのですが、ボタンを押しても音が鳴りません。
11
11
 
12
12
  ### 該当のソース
13
+ ※サンプルを作成しましたので、ViewModelを含めたソースを記載します。
14
+
15
+ ■View
16
+ ```ここに言語を入力
17
+ <Window x:Class="MySoundPlayer.MainWindow"
18
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
19
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
20
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
21
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
22
+ xmlns:vm="clr-namespace:MySoundPlayer.ViewModel"
23
+ mc:Ignorable="d"
24
+ Title="MainWindow" Height="200" Width="200">
25
+
26
+ <Window.DataContext>
27
+ <vm:SoundPlayerViewModel />
28
+ </Window.DataContext>
29
+ <Window.Resources>
30
+ <!--System Button-->
31
+ <Style x:Key="SoundButtonStyle" TargetType="{x:Type ToggleButton}">
32
+ <Setter Property="Template">
33
+ <Setter.Value>
34
+ <ControlTemplate x:Uid="SoundButtonTemplate" TargetType="{x:Type ToggleButton}">
35
+ <Grid x:Name="ButtonImageBrush" Opacity="1" Background="LightGray">
36
+ <Label Name="Caption"
37
+ FontSize="16"
38
+ Content="{Binding ButtonLabel}"
39
+ VerticalContentAlignment="Center"
40
+ HorizontalContentAlignment="Center"/>
41
+ </Grid>
42
+ <ControlTemplate.Triggers>
43
+ <!--<EventTrigger RoutedEvent="Click">
44
+ <SoundPlayerAction Source="/Sounds\button.wav" />
45
+ </EventTrigger>-->
13
- <EventTrigger RoutedEvent="UIElement.PreviewMouseDown">
46
+ <EventTrigger RoutedEvent="Click">
14
- <EventTrigger.Actions>
15
- <SoundPlayerAction Source="{Binding SoundSource}" />
47
+ <SoundPlayerAction Source="{Binding SoundSource}" />
16
- </EventTrigger.Actions>
17
- </EventTrigger>
48
+ </EventTrigger>
49
+ <Trigger Property="IsFocused" Value="True"/>
50
+ <Trigger Property="IsMouseOver" Value="True"/>
51
+ <Trigger Property="IsPressed" Value="True">
52
+ <Setter Property="Background" TargetName="ButtonImageBrush">
53
+ <Setter.Value>
54
+ <SolidColorBrush Color="Gray" />
55
+ </Setter.Value>
56
+ </Setter>
57
+ </Trigger>
58
+ <Trigger Property="IsChecked" Value="True">
59
+ <Setter Property="Background" TargetName="ButtonImageBrush">
60
+ <Setter.Value>
61
+ <SolidColorBrush Color="LightCyan" />
62
+ </Setter.Value>
63
+ </Setter>
64
+ </Trigger>
65
+ </ControlTemplate.Triggers>
66
+ </ControlTemplate>
67
+ </Setter.Value>
68
+ </Setter>
69
+ </Style>
70
+
71
+ </Window.Resources>
72
+ <Grid>
73
+ <ToggleButton Name="BTN_System"
74
+ Width="50"
75
+ Height="50"
76
+ Style="{StaticResource SoundButtonStyle}" />
77
+ </Grid>
78
+ </Window>
79
+ ```
80
+ 上記のコメント部分を有効にすることで、Bindingしなければ音は鳴らせることは確認しました。
81
+
82
+
83
+ ■ViewModel
84
+ ```ここに言語を入力
85
+ using System;
86
+ using System.ComponentModel;
87
+
88
+ namespace MySoundPlayer.ViewModel
89
+ {
90
+ class SoundPlayerViewModel : INotifyPropertyChanged
91
+ {
92
+
93
+ private Uri _SoundSource;
94
+ public Uri SoundSource
95
+ {
96
+ get
97
+ {
98
+ return _SoundSource;
99
+ }
100
+ set
101
+ {
102
+ _SoundSource = value;
103
+ if (PropertyChanged != null)
104
+ {
105
+ PropertyChanged(this, new PropertyChangedEventArgs("SoundSource"));
106
+
107
+ }
108
+ }
109
+ }
110
+
111
+ private String _ButtonLabel;
112
+ public String ButtonLabel
113
+ {
114
+ get
115
+ {
116
+ return _ButtonLabel;
117
+ }
118
+ set
119
+ {
120
+ _ButtonLabel = value;
121
+ if (PropertyChanged != null)
122
+ {
123
+ PropertyChanged(this, new PropertyChangedEventArgs("ButtonLabel"));
124
+
125
+ }
126
+ }
127
+ }
128
+
129
+ public event PropertyChangedEventHandler PropertyChanged;
130
+
131
+ public SoundPlayerViewModel()
132
+ {
133
+ SoundSource = new Uri(@"/Sounds\button.wav", UriKind.Relative);
134
+ ButtonLabel = "Play!";
135
+ }
136
+ }
137
+ }
138
+
139
+ ```