How to access the properties of a class that is within another class?

Asked

Viewed 12 times

0

I have this custom control:

public class CustomButton : Button
{
     public Test Test { get; set; }
}

And this class:

public class Teste
{
     public string MyProperty { get; set; }
}

I want to change this property MyProperty, doing this in C# is very easy, but I have to do this in XAML and it doesn’t work. What I’m using so far is this:

<UserControl.Resources>
    <cc:Teste x:Key="Exemplo" MyProperty="Exemplo"/>
</UserControl.Resources>

<cc:CustomButton Test="{StaticResource Exemplo}"/>

But I get that very common mistake of NullReferenceException. How can I access this property MyProperty and change its value by XAML?

  • The error in C#, not XAML, you need to initialize the object before accessing it. You manipulate data in C#, XMAL is not a programming language.

  • @Maniero I did what you said, I entered the Test class, but there is another problem, in the example I left above, I leave the property Myproperty with the value "Example", I did some tests here and one time the value of Myproperty is simply nothing and another time is the text of Example, never gets only Example.

  • It’s like I have two instances of Test, one hour calls the one with the text, another hour calls the one without text.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.