質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
MVVM

MVVM(Model View ViewModel)は構築上のデザインパターンで、表現ロジック(ViewModel)によってデータ(Model)からページ(View)を分離させます。

XAML

XAML(Extensible Application Markup Language)はWPF、Silverlight、Windows PhoneそしてWindows Store appsでユーザーインターフェースを定義するために使われるXML言語です。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

解決済

1回答

514閲覧

SoundPlayerActionのSourceをバインディングで指定したい

inkan

総合スコア13

MVVM

MVVM(Model View ViewModel)は構築上のデザインパターンで、表現ロジック(ViewModel)によってデータ(Model)からページ(View)を分離させます。

XAML

XAML(Extensible Application Markup Language)はWPF、Silverlight、Windows PhoneそしてWindows Store appsでユーザーインターフェースを定義するために使われるXML言語です。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

0グッド

0クリップ

投稿2019/04/14 10:51

編集2019/04/15 03:00

前提・実現したいこと

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!"; } } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hihijiji

2019/04/15 02:13

ViewModel側のコードを質問欄に提示してください。
inkan

2019/04/15 03:01

コメントありがとうございます。 サンプルのコードを作成し、提示させて頂きました。
guest

回答1

0

ベストアンサー

そこに直接バインドはかけないようですね。
TemplateBinding in EventTrigger @ stackoverflowからBindingProxyクラスを拝借して、SoundSourceはWindow.Window.Resourcesに
BindingProxyのインスタンスを追加(<local:BindingProxy x:Key="proxy" Data="{Binding SoundSource}"/>)して、
そのプロクシ経由でバインド(<SoundPlayerAction Source="{Binding Source={StaticResource proxy}, Path=Data}" />)
すれは取り合えず音が出るみたいです。
リンク先はもうちょっと複雑なことをしていますが…

投稿2019/04/15 05:17

hihijiji

総合スコア4150

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

inkan

2019/04/16 09:05

コメントありがとうございます。 紹介いただいたリンク先を読みましたが、 BindingProxyクラスの内容が今の自分にはきちんと理解できませんでしたので、 引用するにしても、もっと理解を深めてからにしたいと思います。 とりあえず今回は、音源をバインドできないという事実から、 SoundPlayerActionを用いることを諦め、 ViewModel側の処理で音を鳴らすようにしようかなと思います。 もう数日待って、他に回答が無ければ、本回答をベストアンサーに設定させて頂きます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問