I have a C# class like this :
public partial class MyClass: UserControl
{
public String SomeText{get;set;}
...
public MyClass(String Text)
{
this.InitializeComponent();
SomeText = Text;
}
}
And I would like to get the attribute SomeText in my XAML file. How to do something like this ?
<TextBlock Text="{THIS IS HERE I WANT TO HAVE SomeText}"></TextBlock>
I am new to C# so I don’t know how to do. There must be a simple way?
Give the
UserControlelement aNameand then bind to it as in the example below. If your text isn’t going to change you should define it before your call toInitialiseComponent.If your
SomeTextis likely to change then you need to declare it as aDependencyPropertyinstead of a plain oldstringproperty. This would look something like below:You should then change your binding to look like the below, which will cause the UI to update when you change the value of
SomeString.