Lets say I have a really basic class with a few properties. Eg:
public class MyClass
{
public MyClass()
{
Something = "Lorem ipsum dolor sit amet, consectetur adipiscing elit";
OrOther = "Proin dignissim, nunc non tincidunt imperdiet, magna urna malesuada enim";
}
public string Something { get; set; }
public string OrOther { get; set; }
}
And I want to databind to this in Xaml, how would I do this? I've tried binding directly to the object, so in my Xaml page code behind:
public partial class MainPage : ContentPage
{
public MyClass anInstance;
public MainPage()
{
InitializeComponent();
anInstance = new MyClass();
BindingContext = this;
}
}
And then in my Xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:DataBindingTest"
x:Class="DataBindingTest.MainPage">
<Label Text="{Binding anInstance.Something}"
VerticalOptions="Center"
HorizontalOptions="Center" />
And I've also tried setting the BindingContext on a parent control:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:DataBindingTest"
x:Class="DataBindingTest.MainPage">
<StackLayout BindingContext="anInstance">
<Label Text="{Binding Something}"
VerticalOptions="Center"
HorizontalOptions="Center" />
</StackLayout>
</ContentPage>
But it just doesn't work. I've also tried setting the bindingcontext of the page to anInstance:
public partial class MainPage : ContentPage
{
public MyClass anInstance;
public MainPage()
{
InitializeComponent();
anInstance = new MyClass();
BindingContext = anInstance;
}
}
And Just binding to its properties on the xaml side:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:DataBindingTest"
x:Class="DataBindingTest.MainPage">
<Label Text="{Binding Something}"
VerticalOptions="Center"
HorizontalOptions="Center" />
</ContentPage>
But again, all I get are blank pages. It seems that at least one of these should work. Whats the recommended way to bind to properties on a custom class like this?
edit
Taking on board @jason comments, I've also tried this:
public MyClass anInstance
{
get
{
return _anInstance;
}
set
{
_anInstance = value;
}
}
private MyClass _anInstance { get; set; }
public MainPage()
{
InitializeComponent();
anInstance = new MyClass();
BindingContext = this;
}
And the XAML:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:DataBindingTest"
x:Class="DataBindingTest.MainPage">
<StackLayout BindingContext="anInstance">
<Label Text="{Binding Something}"
VerticalOptions="Center"
HorizontalOptions="Center" />
<Label Text="{Binding OrOther}"
VerticalOptions="Center"
HorizontalOptions="Center" />
</StackLayout>
</ContentPage>
But again a blank page...
Same result with:
private MyClass anInstance { get; set; }
public MainPage()
{
InitializeComponent();
anInstance = new MyClass();
BindingContext = this;
}
And
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:DataBindingTest"
x:Class="DataBindingTest.MainPage">
<StackLayout BindingContext="anInstance">
<Label Text="{Binding Something}"
VerticalOptions="Center"
HorizontalOptions="Center" />
<Label Text="{Binding OrOther}"
VerticalOptions="Center"
HorizontalOptions="Center" />
</StackLayout>
</ContentPage>
public MyClass anInstance { get; set; }
. – Diego Rafael Souza