How to best template bind a TriggerAction property
Hey All,
I've recently been refactoring some code in our codebase regarding button SFX playing when a mouse enters it and have largely resolved it but am struggling to do the last 5% and have not found a workable solution online so hoping someone here can help! This is all in Unity using C# just for clarity.
I have written the below code with some back and forth with the intent of playing sound effects in the UI and binding to the Sound property so that other devs can simply bind a value of eSFX (an enum we use for various sound effects).
Below is the intended base style a few other button styles would base themselves upon and as written below the correct sound effect does indeed play, FX_METAL_ROLLOVER6 being one of the enum values of the eSFX enum.
However when I update to the below code, the debug log informs be it's play 'FX_Null' which is the default value of the eSFX enum e.g. Nothing is being correctly assigned.
Here is an example of ho we'd want/expect it to work. The key thing I expected to happen is with the binding of the enum value at the button level, the templatebinding would then update the sound property on the PlaySoundUIAction and play that sound. What I want to understand is why this isn't the case and how to best implement this TriggerAction property in such a way that we can simply bind the enum value at the button level, rather than at the style level, in order to be able to quickly swap what SFX the button players when a mouse enters its UI space.
Thanks in advance for any and all help, I have a feeling it might be simple to resolve but have had little luck in resolving it!
I've recently been refactoring some code in our codebase regarding button SFX playing when a mouse enters it and have largely resolved it but am struggling to do the last 5% and have not found a workable solution online so hoping someone here can help! This is all in Unity using C# just for clarity.
I have written the below code with some back and forth with the intent of playing sound effects in the UI and binding to the Sound property so that other devs can simply bind a value of eSFX (an enum we use for various sound effects).
Code: Select all
public class PlayUISoundAction : TriggerAction<DependencyObject>
{
public static DependencyProperty SoundProperty = DependencyProperty.Register("Sound", typeof(eSFX), typeof(PlayUISoundAction), new PropertyMetadata(eSFX.FX_NULL));
public eSFX Sound
{
get => (eSFX)GetValue(SoundProperty);
set { SetValue(SoundProperty, value); }
}
protected override void Invoke(object parameter)
{
#if NOESIS
UnityEngine.Debug.Log($"The sound selected is: {Sound}");
SFXManager.instance.playUISound((int)Sound);
#endif
}
}
Code: Select all
<Style x:Key="BTN_SH_Glow_Base" TargetType="{x:Type Button}">
<Setter Property="noesis:StyleInteraction.Triggers">
<Setter.Value>
<noesis:StyleTriggerCollection>
<b:EventTrigger EventName="MouseEnter">
<local:PlayUISoundAction Sound="FX_METAL_ROLLOVER6"/>
</b:EventTrigger>
</noesis:StyleTriggerCollection>
</Setter.Value>
</Setter>
</Style>
Code: Select all
<Style x:Key="BTN_SH_Glow_Base" TargetType="{x:Type Button}">
<Setter Property="noesis:StyleInteraction.Triggers">
<Setter.Value>
<noesis:StyleTriggerCollection>
<b:EventTrigger EventName="MouseEnter">
<local:PlayUISoundAction Sound="{TemplateBinding local:PropEx.MouseOverSFX}"/>
</b:EventTrigger>
</noesis:StyleTriggerCollection>
</Setter.Value>
</Setter>
</Style>
Code: Select all
<Button
Name="ButtonResume"
Width="300"
Margin="0,10,0,0"
HorizontalAlignment="Center"
VerticalAlignment="Top"
local:PropEx.TextCentre="{Binding Source={x:Static local:Translate.Instance}, Path=GameTexts[TEXT_GAME_OPTIONS_011]}"
local:PropEx.TextLeft=""
local:PropEx.TextRight=""
local:PropEx.MouseOverSFX="{x:Static Enums:eSFX.FX_METAL_ROLLOVER6}"
Command="{Binding HUDIngameMenu.ButtonIngameMenuCommand}"
CommandParameter="8"
RenderTransformOrigin="0.5,0.5"
Style="{StaticResource BTN_SH_GlowSDisable}"/>
-
-
sfernandez
Site Admin
- Posts: 3363
- Joined:
Re: How to best template bind a TriggerAction property
Hello,
TemplateBinding (or the equivalent Binding with RelativeSource TemplatedParent) should only be used inside a Template to access properties of the templated parent.
In this case you are defining a Style, which sets values directly in the instance where the style is applied. So you should bind using RelativeSource with AncestorType (side note: for referencing attached properties in bindings the syntax requires you to wrap the property in parentheses):
Could you try that?
TemplateBinding (or the equivalent Binding with RelativeSource TemplatedParent) should only be used inside a Template to access properties of the templated parent.
In this case you are defining a Style, which sets values directly in the instance where the style is applied. So you should bind using RelativeSource with AncestorType (side note: for referencing attached properties in bindings the syntax requires you to wrap the property in parentheses):
Code: Select all
<local:PlayUISoundAction Sound="{Binding Path=(local:PropEx.MouseOverSFX), RelativeSource={RelativeSource AncestorType=Button}}"/>Re: How to best template bind a TriggerAction property
Hey There sfernandez,
First of all I'd like to apologise for the delayed response, I've been fixing another project so have only just been able to return to check this.
This has exactly fixed my issue, it sounds like I hadn't completely grasped it as I typically also have a ControlTemplate in a number of styles and broadly assumed it's usage elsewhere. Good to know that a style is basically instantiated when an instance is created so that merits using relative binding.
Thanks again so much!
First of all I'd like to apologise for the delayed response, I've been fixing another project so have only just been able to return to check this.
This has exactly fixed my issue, it sounds like I hadn't completely grasped it as I typically also have a ControlTemplate in a number of styles and broadly assumed it's usage elsewhere. Good to know that a style is basically instantiated when an instance is created so that merits using relative binding.
Thanks again so much!
Re: How to best template bind a TriggerAction property
Thanks for the feedback!
Who is online
Users browsing this forum: Bing [Bot] and 7 guests