Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Forms: bind to a code behind property in XAML

In Xamarin.Forms I would like to bind a code behind property to a label in XAML.

I found many answers and web pages about this topic, but they all cover more complex scenarios.

This is my XAML page:

<?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:TrackValigie"
             x:Class="TrackValigie.SelViaggioPage">
    <ContentPage.Content>
            <StackLayout>
                <Label Text="{Binding ?????????}" />
            </StackLayout>
    </ContentPage.Content>
</ContentPage>

And this is code behind:

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SelViaggioPage : ContentPage
{

    private string _lblText;
    public string LblText
    {
        get
        {
            return _lblText;
        }
        set
        {
            _lblText = value;
            OnPropertyChanged();
        }
    }

    public SelViaggioPage()
    {
        InitializeComponent();
    }

    protected override void OnAppearing()
    {

        this.LblText = "Ciao!!";

        base.OnAppearing();
    }
}

I would like to bind the "LblText" property to the label, using XAML only, that means without setting binding or binding context in code behind.

Is this possible?

like image 284
Disti Avatar asked Jan 13 '19 16:01

Disti


People also ask

How do you bind in code behind Xamarin forms?

The binding references the source object. To set the data binding, use the following two members of the target class: The BindingContext property specifies the source object. The SetBinding method specifies the target property and source property.

What is binding context in Xamarin forms?

When working with XAML in Xamarin. Forms, we use data-binding to connect properties on a binding context (such as a ViewModel) to controls in a page or a view. This powerful feature lets changes in the view automatically change the view model and vice-versa. However, the binding context is a runtime concept.

How does binding work in Xamarin forms?

Data binding is the technique of linking properties of two objects so that changes in one property are automatically reflected in the other property. Data binding is an integral part of the Model-View-ViewModel (MVVM) application architecture.

What is two way binding in Xamarin forms?

However, the default binding mode for the Value property of Slider is TwoWay . This means that when the Value property is a data-binding target, then the target is set from the source (as usual) but the source is also set from the target. This is what allows the Slider to be set from the initial Opacity value.


1 Answers

your page will need to implement INotifyPropertyChanged, but the binding syntax should just be

<ContentPage x:Name="MyPage" ... />

... 

<Label BindingContext="{x:Reference Name=MyPage}" Text="{Binding LblText}" />
like image 160
Jason Avatar answered Oct 12 '22 03:10

Jason