Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9021621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:17:56+00:00 2026-06-16T05:17:56+00:00

I have the following Binding in an EventTrigger: <ControlTemplate.Triggers> <EventTrigger RoutedEvent=PreviewMouseDown> <SoundPlayerAction Source={Binding Path=SoundFile,

  • 0

I have the following Binding in an EventTrigger:

<ControlTemplate.Triggers>
    <EventTrigger RoutedEvent="PreviewMouseDown">
        <SoundPlayerAction Source="{Binding Path=SoundFile, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource soundFileConverter}}" />
    </EventTrigger>
    ...

The process is the following: The custom control (that’s its template) has a property named SoundFile, an enum type. In the converter this enum value should be converted to an Uri to pass it to the SoundPlayerAction.

Here’s the problem: The converter isn’t called anyway. The output window presents the following error:

Cannot find governing FrameworkElement or FrameworkContentElement for target element.
BindingExpression:Path=SoundFile; DataItem=null; target element is ‘SoundPlayerAction’
HashCode=46763000); target property is ‘Source’ (type ‘Uri’)

What’s wrong with the binding expression?

EDIT:

For a better overview, here’s the entire template of the control:

<Style TargetType="{x:Type controls:Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type controls:Button}">
                <Border Name="Border"
                        Background="{TemplateBinding Background}"
                        BorderBrush="Transparent"
                        BorderThickness="0">
                    <Border.CornerRadius>
                        <MultiBinding Converter="{StaticResource areaCornerRadiusConverter}">
                            <MultiBinding.Bindings>
                                <Binding Path="RoundType" RelativeSource="{RelativeSource TemplatedParent}" />
                                <Binding Path="ActualHeight" RelativeSource="{RelativeSource TemplatedParent}" />
                            </MultiBinding.Bindings>
                        </MultiBinding>
                    </Border.CornerRadius>
                    <TextBlock Margin="{Binding Path=RoundType,
                                                RelativeSource={RelativeSource TemplatedParent},
                                                Converter={StaticResource buttonMarginConverter}}"
                               FontSize="{TemplateBinding FontSize}"
                               Style="{StaticResource innerTextBlock}"
                               Text="{TemplateBinding Text}" />
                </Border>
                <ControlTemplate.Triggers>
                    <EventTrigger RoutedEvent="PreviewMouseDown">
                        <SoundPlayerAction Source="{Binding Path=SoundFile, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource soundFileConverter}}" />
                    </EventTrigger>                        
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

EDIT 2:

I tried it in another way: Setting the name attribute of the SoundPlayerAction to PART_SoundPlayerAction and retrieving it from the code-behind with GetTemplateChild. But GetTemplateChild returns always null. That’s really annoying. Nothing seems to work…

EDIT 3:

Now with the answer of Blachshma I got it that the converter is called during the initalization of the control. But not when the property is changing. Furthermore, the value which is returned by the converter, isn’t applied as Source to the SoundPlayerAction.

I implemented the BindingProxy:

public class BindingProxy : Freezable
{
    #region Overrides of Freezable

    protected override Freezable CreateInstanceCore()
    {
        return new BindingProxy();
    }

    #endregion

    public SoundFile Data
    {
        get { return (SoundFile)GetValue(DataProperty); }
        set { SetValue(DataProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DataProperty =
        DependencyProperty.Register("Data", typeof(SoundFile), typeof(BindingProxy), new UIPropertyMetadata(SoundFile.None));
}

And I changed the Path=Data.SoundFile to Path=Data. Is there any mistake?

EDIT 4:

The solution with the MakeSoundCommand is working perfectly. Thanks a lot to Blachshma.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:17:57+00:00Added an answer on June 16, 2026 at 5:17 am

    Well you’re right, trying to use binding in that specific place (An EventTrigger inside a style) is quite a pain.

    I’ve found that the best way to solve this issue is to use both Freezables (to inherit the DataContext) together with static BindingProxies…

    To solve it in your case, start by making a freezable class, we’ll call it BindingProxy:

    public class BindingProxy : Freezable
    {
        #region Overrides of Freezable
    
        protected override Freezable CreateInstanceCore()
        {
            return new BindingProxy();
        }
    
        #endregion
    
        public object Data
        {
            get { return (object)GetValue(DataProperty); }
            set { SetValue(DataProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DataProperty =
            DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
    }
    

    So our BindingProxy class implements Freezable and exposes a property named Data. This will be the Property which will hold the DataContext.

    Now, in our resources, we’ll create a StaticResource that uses this class… We’ll name it “proxy“:

    <Window.Resources>
        <local:BindingProxy x:Key="proxy" Data="{Binding}" />
    

    Notice, that we bind the Window’s DataContext into the Data property. This will allow use to access it from the SoundPlayerAction.

    Finally, let’s update the SoundPlayerAction to work with our proxy:

    <EventTrigger RoutedEvent="PreviewMouseDown">
        <SoundPlayerAction Source="{Binding Source={StaticResource proxy}, Path=Data.SoundFile,Converter={StaticResource soundFileConverter}}" />
    </EventTrigger>   
    

    Instead of the regular binding you used, we’re binding to a StaticResource, the instance of our BindingProxy class. Since the Data property is binded to the DataContext of the window, we can get the SoundFile property using Data.SoundFile.
    And as an added bonus, since all this is done via Source={Binding you can stil call your soundFileConverter to convert the string to a URI.

    Update:
    OP does not want to put the BindingProxy class inside some <Window.Resources> tag every time he uses this control (which is legitimate), so in this version, we’re going to put all the logic inside the ResourceDictionary and modify a bit of XAML so that it continues to work..

    So in our resource dictionary, we’re going to use System.Windows.Interactivity and Microsoft.Expression.Interactions (both freely available through the Blend SDK)

    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    

    Since in the previous version we could count on the binding being preformed through the Window.Resources, this time we’re going to have to do it ourselves… We’ll modify the original template to add an <i:Interaction.Triggers> which will replace the EventTrigger but allows sounds to play through a dedicated command:

    <local:MakeSoundCommand x:Key="soundCommand"/>
    <Style  TargetType="{x:Type controls:Button}" >
      ....
      ....
       <i:Interaction.Triggers>
         <i:EventTrigger EventName="PreviewMouseDown">
            <local:EventToCommand Command="{StaticResource soundCommand}" CommandParameter="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SoundFile, Converter={StaticResource soundFileConverter}}" />
         </i:EventTrigger>
       </i:Interaction.Triggers>
       <TextBlock Margin="{Binding Path=RoundType,....
    

    This is placed right before the TextBlock, what it does is handle the PreviewMouseDown event and calls the a command named soundCommand of type MakeSoundCommand.

    This is the implementation of MakeSoundCommand:

    public class MakeSoundCommand : ICommand
    {
        public bool CanExecute(object parameter)
        {
            return true;
        }
    
        public event EventHandler CanExecuteChanged;
    
        public void Execute(object parameter)
        {
            Uri uri = parameter as Uri;
    
            if (uri != null)
            {
                StreamResourceInfo sri = Application.GetResourceStream(uri);
                SoundPlayer simpleSound = new SoundPlayer(sri.Stream);
                simpleSound.Play();
            }
        }
    

    The rest of the code remains the same.
    Note: The EventToCommand used is the one from the MVVM Light Toolkit and can be downloaded here

    Final Result:

    Generic.xaml:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
                    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
                    xmlns:local="<YOUR NAMESPACE>">
    
    <local:MakeSoundCommand x:Key="soundCommand" />
    <local:SoundFileToUriConverter:Key="soundFileConverter" />
    <Style TargetType="{x:Type controls:Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType=" {x:Type controls:Button}">
                    <Border Name="Border"
                        Background="{TemplateBinding Background}"
                        BorderBrush="Transparent"
                        BorderThickness="0">
                        <Border.CornerRadius>
                          ....
                        </Border.CornerRadius>
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="PreviewMouseDown">
                              <local:EventToCommand Command="{StaticResource soundCommand}" CommandParameter="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SoundFile, Converter={StaticResource soundFileConverter}}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                        <TextBlock Margin="{Binding Path=RoundType,
                                                RelativeSource={RelativeSource TemplatedParent}}" .... />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with the following binding: <telerik:RadComboBox ItemsSource={Binding Source={StaticResource TemplateDataSource}, Path=Templates} SelectedValue={Binding
I have a list view as following: <ListView x:Name=lvLedger Height={Binding Path=GridHight, ElementName=ledgerList} Width={Binding Path=GridWidth,
I have an ItemsControl with the following ItemTemplate: <DataTemplate x:Key=myItemTemplate> <TextBlock Height=??? Text={Binding Path=Description}
I have the following XAML: <Grid x:Name=LayoutRoot Background=White DataContext={Binding Source={StaticResource MyDataKey}}> <TextBox Name=_myId Text={Binding
I have the following structure in XAML: <Grid> <ItemsControl x:Name=Items ItemsSource={Binding Path=Pages} Grid.Row=0 Grid.Column=0>
I have the following Data template with triggers: <DataTemplate.Triggers> <DataTrigger Binding={Binding IsCalled} Value=Yes> <Setter
I have created a Button with the following trigger: <Button Content=Test> <i:Interaction.Triggers> <i:EventTrigger EventName=Click>
I have following collection view <CollectionViewSource x:Key=messages Source={Binding src}> <CollectionViewSource.GroupDescriptions> <PropertyGroupDescription PropertyName=Group/> </CollectionViewSource.GroupDescriptions> </CollectionViewSource>
let's say I have following code : <ContextMenu IsEnabled={Binding Converter={StaticResource SomeConverterWithSmartLogic}}> So, I did
I have following DataGrid in wpf. <DataGrid AutoGenerateColumns=False Grid.Row=1 Name=adsGrid ItemsSource={Binding Path=Ads} CanUserAddRows=False CanUserDeleteRows=False

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.