armin.it
Topic Author
Posts: 8
Joined: 31 Jul 2020, 16:15

Set property by name

01 Aug 2020, 13:00

Hi,
it is possible to set a property of a control using the name of the property?

Like
Control->Setproperty(„Background“,“Red“)
 
User avatar
jsantos
Site Admin
Posts: 2906
Joined: 20 Jan 2012, 17:18
Contact:

Re: Set property by name

03 Aug 2020, 11:52

Yes, we are doing that internally when we parse XAMLs. You can use our internal reflection architecture to do this. If you provide more context about it I can provide you more detail.
 
armin.it
Topic Author
Posts: 8
Joined: 31 Jul 2020, 16:15

Re: Set property by name

03 Aug 2020, 12:14

A client-program sends message to my C++ GUI server program, and need's to get and set propertys of Controls of the GUI. The message contains the name of the control and the
name of the property as strings, for example "Button01.set.background=Red" or "Textbox05.get.Text"

I'm already able to find the right Control with

BaseComponent* lComponent = this->FindName("Button01");
const Noesis::TypeClass *lT = lComponent->GetClassType();
const char *lN = lT->GetName();
 
User avatar
jsantos
Site Admin
Posts: 2906
Joined: 20 Jan 2012, 17:18
Contact:

Re: Set property by name

03 Aug 2020, 19:46

Given a TypeClass, you can obtain the UIElementData medata.
TypeClass* type = TypeOf<Button>();
UIElementData* data = type->GetMetaData().Find<UIElementData>();
UIElementData provides methods for finding dependency properties (like FindProperty).
 
armin.it
Topic Author
Posts: 8
Joined: 31 Jul 2020, 16:15

Re: Set property by name

03 Aug 2020, 20:44

My test didn't work, FindPorperty returns always a Null-Pointer ?

Ptr<BaseComponent> lNew = Factory::CreateComponent(Symbol("Button"));
const Noesis::TypeClass *lT = lNew->GetClassType();
UIElementData* lData = lT->GetMetaData().Find<UIElementData>();
const Noesis::DependencyProperty *lP = lData->FindProperty(Symbol("FontSize"));
 
User avatar
jsantos
Site Admin
Posts: 2906
Joined: 20 Jan 2012, 17:18
Contact:

Re: Set property by name

03 Aug 2020, 21:38

I see, you need to traverse up the hierarchy using TypeClass::GetBase().
 
armin.it
Topic Author
Posts: 8
Joined: 31 Jul 2020, 16:15

Re: Set property by name

04 Aug 2020, 08:38

I found the function "FindDependencyProperty" with seems to find the property's, but i don't understand how I can set and get
the value of the property, because I don't understand the needed arguments of the function GetValue.. and SetValue ...

Is there any example or code snippet ?

Ptr<BaseComponent> lNew = Factory::CreateComponent(Symbol("Button"));
const Noesis::DependencyProperty *p0 = Noesis::FindDependencyProperty(lNew->GetClassType(), Symbol("Background"));
const Noesis::DependencyProperty *p1 = Noesis::FindDependencyProperty(lNew->GetClassType(), Symbol("FontFamily"));
const Noesis::DependencyProperty *p2 = Noesis::FindDependencyProperty(lNew->GetClassType(), Symbol("FontSize"));
const Noesis::DependencyProperty *p3 = Noesis::FindDependencyProperty(lNew->GetClassType(), Symbol("Content"));
 
User avatar
jsantos
Site Admin
Posts: 2906
Joined: 20 Jan 2012, 17:18
Contact:

Re: Set property by name

04 Aug 2020, 10:42

Something like this should work:
button->SetValue<float>(property, 12.0f);
 
armin.it
Topic Author
Posts: 8
Joined: 31 Jul 2020, 16:15

Re: Set property by name

04 Aug 2020, 13:16

... that's means that I must prior know the datatype of the property for specifing the template (for example <float> ) in my code.

Another hint for me is that the property FontFamily returns as Type "FontFamily", but setting "FontFamily" in the brackets of the template
didn't work

Are there any Converter like in C#/WPF so that I can do the same with noesisgui c++

ValueString = "Arial"
var converter = TypeDescriptor.GetConverter(propType);
var convertedObject = converter.ConvertFromString(ValueString);
lProperty.SetValue(lControl, convertedObject);
 
User avatar
jsantos
Site Admin
Posts: 2906
Joined: 20 Jan 2012, 17:18
Contact:

Re: Set property by name

04 Aug 2020, 13:33

Yes, sorry I cannot give you code for this right now, but these are the steps you need to follow:

1. Get a converter for a type using TypeConverter::Create()
2. Use the converter method TryConvertFromString
3. The converter gives you a BaseComponent that you can set in the DependencyObject using SetValueObject

Who is online

Users browsing this forum: No registered users and 1 guest