Jarryd
Topic Author
Posts: 14
Joined: 05 Sep 2023, 07:25

b:DataTrigger not working with enums

08 Feb 2024, 05:35

So I've got a b:DataTrigger that's supposed to go off when a bound value that is an enum is a the right case.
...
    <b:Interaction.Triggers>
        
        <b:DataTrigger Binding="{Binding SignInTransitionState}" Value="SignInIntro">
            <b:GoToStateAction StateName="SignInIntroState" TargetName="RootGrid" />
        </b:DataTrigger>
        
    </b:Interaction.Triggers>    
    
</Page>
I've got a C# implementation, for sanity checking everything.
In Blend, this all works perfectly when I run my Workspace solution.
And here's my enum in c#.
namespace Workspace.Enums
{
    public enum SignInTransitionStates
    {
        SignInIntro = 0,
        SignInIdle,
        SignInOutro,

        MainMenuIntro,
        MainMenuIdle,

        Gameplay,
        COUNT

    }
}
Meanwhile, over in the actual game running in noesis unmanaged, I'm doing the same thing.
And the triggers do not fire.
The triggers think my bound value is an int.
So if I register my enum instance using a pair of get\set accessors that cast to int... and then change my XAML triggers to listen for integers, it works!
...
        <b:Interaction.Triggers>
            <b:DataTrigger Binding="{Binding SignInTransitionState}" Value="0">
                <b:GoToStateAction StateName="SignInIntroState" TargetName="RootGrid" />
            </b:DataTrigger>
        </b:Interaction.Triggers>

</Page>
Here's the C++ snippets:
NS_DECLARE_REFLECTION_ENUM(NoesisEnums::SignInTransitionStates)

	enum class SignInTransitionStates : int
	{
		SignInIntro = 0,
		SignInIdle,
		SignInOutro,
		MainMenuIntro,
		MainMenuIdle,
		Gameplay,

		COUNT
	};

NS_IMPLEMENT_REFLECTION_ENUM(NoesisEnums::SignInTransitionStates, "Workspace.Enums.SignInTransitionStates")
{
	NsVal("SignInIntro", NoesisEnums::SignInTransitionStates::SignInIntro);
	NsVal("SignInIdle", NoesisEnums::SignInTransitionStates::SignInIdle);
	NsVal("SignInOutro", NoesisEnums::SignInTransitionStates::SignInOutro);	
	NsVal("MainMenuIntro", NoesisEnums::SignInTransitionStates::MainMenuIntro);
	NsVal("MainMenuIdle", NoesisEnums::SignInTransitionStates::MainMenuIdle);	
	NsVal("Gameplay", NoesisEnums::SignInTransitionStates::Gameplay);
	NsVal("COUNT", NoesisEnums::SignInTransitionStates::COUNT);
}

NoesisEnums::SignInTransitionStates m_signInTransitionState = NoesisEnums::SignInTransitionStates::MainMenuIntro;

int paNVMBackground::GetSignInTransitionStateFromInt() const 
{
	return (int)m_signInTransitionState;

}

void paNVMBackground::SetSignInTransitionStateFromInt(int transitionState)
{
	m_signInTransitionState = (NoesisEnums::SignInTransitionStates)transitionState;
	OnPropertyChanged(SignInTransitionState_NoesisName);
}
 
User avatar
sfernandez
Site Admin
Posts: 2997
Joined: 22 Dec 2011, 19:20

Re: b:DataTrigger not working with enums

08 Feb 2024, 14:28

It is possible that you are not registering the converter for the enum:
RegisterComponent<EnumConverter<SignInTransitionStates>>();
You need to do that in order for the XAML parser to understand the string used in Value="SignInIntro".

We are doing this in our Menu3D demo:
https://github.com/Noesis/Tutorials/blo ... in.cpp#L65
https://github.com/Noesis/Tutorials/blo ... .xaml#L581

Could you try that?
 
Jarryd
Topic Author
Posts: 14
Joined: 05 Sep 2023, 07:25

Re: b:DataTrigger not working with enums

12 Feb 2024, 07:02

Thank you, this is the information I needed to figure it out and get it working.
I had a few issues going on:

The enum Types were not being registered.

A component needed to be registered, that contained a noesis::EnumConverter<MyEnum>, so it could figure out how to get a string to be become my enum type.

My enums were manually being declared as derived from Ints, which interfered somehow with the conversion process within the xaml. I just stripped the :
 : int
And now that these were straightened out, I could go back to the trigger, and make the value specify the type explicitly instead by using x:type of lazily relying on implicit string conversions. Which means intellisense to be sure I'm locked onto the right enum value.
            <DataTrigger Binding="{Binding ClothesType}" Value="{x:Static vm:OutfitType.Pants}"> 
                <Setter TargetName="ClothesImage" Property="Source" Value="{StaticResource PantsImageSource}"/> 
            </DataTrigger> 
I appreciate the help! ☺

Who is online

Users browsing this forum: No registered users and 4 guests