Page 1 of 1

Enum Dependency Property

Posted: 29 Jun 2024, 22:57
by epfor
hi and thanks inAdvance:

i created a user control with some dependency properties.
in the end i want this line to work:
        <local:SettingsUserControl Name="SettingModule" Background="White" Grid.Row="1" Grid.Column="0" Margin="10"
                                     HorizontalAlignment="Stretch" VerticalAlignment="Stretch" [u][b]LowerButtonTypeProperty="local:LowerButtonType.PowerButton">[/b][/u]
i use this enum:
namespace XXX
{
	enum class LowerButtonType   {
		PowerButton,
		BackButton,
		NumButtons
	};

}

NS_IMPLEMENT_INLINE_REFLECTION_ENUM(XXX::LowerButtonType, "XXX.LowerButtonType")
{
NsVal("PowerButton", XXX::LowerButtonType::PowerButton);
NsVal("BackButton", XXX::LowerButtonType::BackButton);

}

register the property:
	NS_IMPLEMENT_INLINE_REFLECTION(SettingsUserControl, UserControl, "XXX.SettingsUserControl")
		{
			Noesis::UIElementData* data = NsMeta<Noesis::UIElementData>(Noesis::TypeOf<SelfClass>());

			data->RegisterProperty<LowerButtonType>(lower_button_type_property, "LowerButtonTypeProperty", Noesis::PropertyMetadata::Create(LowerButtonType(LowerButtonType::BackButton)));

		}
also declared and defined in hpp and cpp as static.
this is the relevent part in my user control xaml:
     <Image Source="{Binding LowerButtonTypeProperty, Converter={StaticResource ImageConverter}, RelativeSource={RelativeSource AncestorType=UserControl}}">
i tested my converter and i works.
i made other dependency properties bool type and it also work.
the error i get is:
MainWindow.xaml(64,80): Cannot set value for property 'BasicAssistantScreen.SettingsUserControl.LowerButtonTypeProperty', 'local:LowerButtonType.PowerButton' is not a valid value

i tried also this:
        <local:SettingsUserControl Name="SettingModule" Background="White" Grid.Row="1" Grid.Column="0" Margin="10"
                                     HorizontalAlignment="Stretch" VerticalAlignment="Stretch" LowerButtonTypeProperty="PowerButton">
i try to find what i do wrong.
i am pretty new in noesis so maybe some funny mistake.

thanks again

Re: Enum Dependency Property

Posted: 02 Jul 2024, 07:40
by sfernandez
Hi, it seems you haven't registered the converter for the enum.
RegisterComponent<EnumConverter<XXX::LowerButtonType>>();
Could you try that?

Re: Enum Dependency Property

Posted: 03 Jul 2024, 22:44
by epfor
hi,
thanks for the answer.
i actually forgot to register and when i registered it solved another problem i had but not this.
someone told me to implement converter from string to LowerButtonType. i will try to do it and update if it works.
if i understood the rational behind was like that on of the conversion (i guess implicit) betwwen True False to boolean, which worked on my boolean properties.

Re: Enum Dependency Property

Posted: 04 Jul 2024, 13:22
by sfernandez
I forgot to mention that you should assign the enum property using just the string value, or the enum value with x:Static:
<local:SettingsUserControl LowerButtonTypeProperty="PowerButton">
<local:SettingsUserControl LowerButtonTypeProperty="{x:Static local:LowerButtonType.PowerButton}">

Re: Enum Dependency Property

Posted: 04 Jul 2024, 13:42
by epfor
Thank you!!
the x: Static worked for me.

can you please explain what you meant with the string? i do not understand
did you mean that if i write this:

<local:SettingsUserControl LowerButtonTypeProperty="PowerButton">

i need to create another converter from string to LowerButtonTypeProperty as i was told?

Re: Enum Dependency Property

Posted: 04 Jul 2024, 13:54
by sfernandez
No need to write a custom converter, if you registered the EnumConverter<XXX::LowerButtonType>> then you can directly write the enum string value in the xaml, as I posted above:
<local:SettingsUserControl LowerButtonTypeProperty="PowerButton">

Re: Enum Dependency Property

Posted: 04 Jul 2024, 17:50
by epfor
OK,
i misunderstood you on you first answer.

When I do as you offered as you offered:

Noesis::RegisterComponent<EnumConverter<BasicAssistantScreen::LowerButtonType>>();

i got:
Use of undeclared identifier 'EnumConverter'

should i use some include or implement the converter?

i looked in the forum and saw your same suggestion in many answers which worked so i guess i miss something small.
thanks.

Re: Enum Dependency Property

Posted: 04 Jul 2024, 19:19
by sfernandez
You'll need to include the EnumConverter header:
#include <NsCore/EnumConverter.h>
and maybe use the Noesis namespace if you don't define the corresponding using namespace:
Noesis::RegisterComponent<Noesis::EnumConverter<BasicAssistantScreen::LowerButtonType>>();

Re: Enum Dependency Property

Posted: 05 Jul 2024, 10:06
by epfor
Worked!

thank you!