Page 1 of 1

DependencyProperty with string value

Posted: 05 Sep 2013, 05:33
by Daki
What is the proper way to implement a DependencyProperty that represents a string value? I have a simple UserControl object that is basically just some text blocks whose text I want to expose with Dependency Properties so I may set it programmatically. I have these properties as NsStrings right now but I get compilation errors when using NsProp.

Should the values be cast to NsStrings, or should it be another type? Do the function pointers provided to NsProp need to be of a certain signature? Right now for my setter I am passing in a NsString which I don't really want to do (I'd rather pass in a const NsString& or an NsString*).

I'm curious what the best way to do this is - the examples only seem to cover numeric types.

Re: DependencyProperty with string value

Posted: 06 Sep 2013, 00:26
by jsantos
The functions passed to NsProp must use const NsString&:
const NsString& MyClass::GetText() const
{
    return GetValue<NsString>(TextProperty);
}

void MyClass::SetText(const NsString& text)
{
    SetValue<NsString>(TextProperty, text);
}

...
NsProp("Text", &MyClass::GetText, &MyClass::SetText);
The property is registered using the NsString type:
data->RegisterProperty<NsString>(TextProperty, "Text",
    FrameworkPropertyMetadata::Create(NsString(), FrameworkOptions_None));

Re: DependencyProperty with string value

Posted: 06 Sep 2013, 01:00
by Daki
Thanks! :)

Re: DependencyProperty with string value

Posted: 06 Sep 2013, 02:39
by Shorinji
Hi,
I have a question which could be related this.
I also have some UserControl contaning some strings.
Right now i am using string not NsString, and it's working. Is it correct to use string for dependcy properties in Unity?
Thanks in advance.

Re: DependencyProperty with string value

Posted: 06 Sep 2013, 12:21
by sfernandez
Hi,
I have a question which could be related this.
I also have some UserControl contaning some strings.
Right now i am using string not NsString, and it's working. Is it correct to use string for dependcy properties in Unity?
Thanks in advance.
Yes, mono strings are automatically converted to native NsString, so that is the correct way to define a string property in Unity.

Re: DependencyProperty with string value

Posted: 06 Sep 2013, 16:19
by Shorinji
ok thanks :)