Hi,
I would like to bind an object with its properties to a StackLayout inside my ContentPage.
So basically I have an object with two properties Name and Lastname that is a class property inside my Page.xaml.cs:
I tried different ways to bind it, but all causes the application to just close/crash. So lets say this is my incorrect data-bind implementation:
//Stacklayout inside XAML
<StackLayout x:Name="TestLayout" BindingContext="{Data}">
<Label Text="{Binding Name}"/>
<Label Text="{Binding Lastname}"/>
</StackLayout>
//My object
public class MyObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string Name
{
get
{
return Name;
}
set
{
Name = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(nameof(Name)));
}
}
}
public string Lastname
{
get
{
return Lastname;
}
set
{
Lastname = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(nameof(Lastname)));
}
}
}
public MyObject(string name, string lastname)
{
Name = name;
Lastname = lastname;
}
}
//My page
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MyPage : ContentPage
{
private MyObject Data;
public Unloading(string env, string username, string name, string call, PageOptions options)
{
InitializeComponent();
Data = new MyObject("John", "Doe");
}
}
How do I need to set BindingContext in order for it to work?
BR,
Denis