How to bind? #74
-
I was trying to bind a label's This is the code I've written and I've tried all kinds of different variations of this but non work, so clearly I don't understand how binding works. However, I haven't found any info on the internet on how to make this simple binding. I've only seen people use an event handler or the sample code here which I'm trying to immitate, but evidently failing. Can someone explain this for me? 🙏
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hey @PlayCreatively, We do link over to the .NET MAUI docs but I don't actually see anything clearly helpful there either. Essentially when creating a binding we need to do so against a property that is accessible ( What you had was close but there are some extra bits you needed. See this modified version of your code: class MyPage : ContentPage
{
public string MyVal { get; } = "initial value";
public DataPage()
{
BindingContext = this;
Content =
new VerticalStackLayout()
{
Children =
{
new Label().Bind(Label.TextProperty, path: nameof(MyPage.MyVal))
}
};
}
} The changes I made are:
|
Beta Was this translation helpful? Give feedback.
-
Closed and answered |
Beta Was this translation helpful? Give feedback.
Hey @PlayCreatively,
We do link over to the .NET MAUI docs but I don't actually see anything clearly helpful there either. Essentially when creating a binding we need to do so against a property that is accessible (
public
orinternal
), to tell the control the context of the binding (BindingContext
) and we also need a way of notifying the control when things change (INotifyPropertyChanged
).What you had was close but there are some extra bits you needed. See this modified version of your code: