I'm having problems binding text to a label inside a custom control.
I have made the following control:
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Frida3.Components.TestView">
<ContentView.Content>
<StackLayout>
<Label Text="{Binding Text}" />
</StackLayout>
</ContentView.Content>
</ContentView>
With the following code-behind:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TestView : ContentView
{
public TestView ()
{
InitializeComponent ();
BindingContext = this;
}
public static readonly BindableProperty TextProperty =
BindableProperty.Create("Text", typeof(string), typeof(TestView), default(string));
public string Text
{
get => (string) GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
}
When using the control and a binding to set the Text property, nothing is shown. Below is some sample code of showing the results:
<!-- Shows that the LoginText binding contains value -->
<Label Text="{Binding LoginText}" BackgroundColor="BurlyWood"/>
<!-- Nothing shown with same binding -->
<components:TestView Text="{Binding LoginText}" BackgroundColor="Aqua" />
<!-- Works without binding -->
<components:TestView Text="This is showing" BackgroundColor="Yellow" />
And here is the result of that: