前提・実現したいこと
WPFでアプリを作っています。
XAMLにて、特定のボタンが押された際に、SoundPlayerActionで音を鳴らしています。
Source属性に音源ファイルのパスをハードコーディングすることで、音が鳴らせることは確認しました。
今回、この音をXAMLのハードコーディングではなく、ViewModelからバインディングして指定するようにしようとしています。
発生している問題・エラーメッセージ
UriクラスのSoundSourceプロパティをViewModelに定義し、バインディングしているのですが、ボタンを押しても音が鳴りません。
該当のソース
※サンプルを作成しましたので、ViewModelを含めたソースを記載します。
■View
<Window x:Class="MySoundPlayer.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:vm="clr-namespace:MySoundPlayer.ViewModel" mc:Ignorable="d" Title="MainWindow" Height="200" Width="200"> <Window.DataContext> <vm:SoundPlayerViewModel /> </Window.DataContext> <Window.Resources> <!--System Button--> <Style x:Key="SoundButtonStyle" TargetType="{x:Type ToggleButton}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate x:Uid="SoundButtonTemplate" TargetType="{x:Type ToggleButton}"> <Grid x:Name="ButtonImageBrush" Opacity="1" Background="LightGray"> <Label Name="Caption" FontSize="16" Content="{Binding ButtonLabel}" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/> </Grid> <ControlTemplate.Triggers> <!--<EventTrigger RoutedEvent="Click"> <SoundPlayerAction Source="/Sounds\button.wav" /> </EventTrigger>--> <EventTrigger RoutedEvent="Click"> <SoundPlayerAction Source="{Binding SoundSource}" /> </EventTrigger> <Trigger Property="IsFocused" Value="True"/> <Trigger Property="IsMouseOver" Value="True"/> <Trigger Property="IsPressed" Value="True"> <Setter Property="Background" TargetName="ButtonImageBrush"> <Setter.Value> <SolidColorBrush Color="Gray" /> </Setter.Value> </Setter> </Trigger> <Trigger Property="IsChecked" Value="True"> <Setter Property="Background" TargetName="ButtonImageBrush"> <Setter.Value> <SolidColorBrush Color="LightCyan" /> </Setter.Value> </Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <ToggleButton Name="BTN_System" Width="50" Height="50" Style="{StaticResource SoundButtonStyle}" /> </Grid> </Window>
上記のコメント部分を有効にすることで、Bindingしなければ音は鳴らせることは確認しました。
■ViewModel
using System; using System.ComponentModel; namespace MySoundPlayer.ViewModel { class SoundPlayerViewModel : INotifyPropertyChanged { private Uri _SoundSource; public Uri SoundSource { get { return _SoundSource; } set { _SoundSource = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("SoundSource")); } } } private String _ButtonLabel; public String ButtonLabel { get { return _ButtonLabel; } set { _ButtonLabel = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ButtonLabel")); } } } public event PropertyChangedEventHandler PropertyChanged; public SoundPlayerViewModel() { SoundSource = new Uri(@"/Sounds\button.wav", UriKind.Relative); ButtonLabel = "Play!"; } } }
回答1件
あなたの回答
tips
プレビュー