Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xamarin.forms binding from xaml to property

I am a total newbie with bindings in xaml and I really don't get it sometimes.

I have this in my xaml:

<ActivityIndicator IsRunning="{Binding IsLoading}" IsVisible="{Binding IsLoading}" />

The binding "IsLoading". Where do I declare/set this property?!

My .cs looks like this:

....
    public bool IsLoading;

    public CardsListXaml ()
    {
        InitializeComponent ();
        IsLoading = true;
 ....
like image 814
ullstrm Avatar asked Sep 18 '14 12:09

ullstrm


People also ask

How do you bind property in 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 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.

What is binding context in Xamarin forms?

Binding, in the most common context, is the process of mapping a property on a Page, to a property in a ViewModel. In Xamarin Forms terms, the Page is a BindableObject and the BindableObject has a BindingContext, which would be the ViewModel.

Do you have to use XAML in Xamarin forms?

XAML is never required in a Xamarin. Forms program, but it is often more succinct and more visually coherent than equivalent code, and potentially toolable.


1 Answers

Bindings are typically resolved from the BindingContext property (in other implementations, this property is called DataContext). This is null by default (at least in other implementations of XAML), thus your view is unable to find the specified properties.

In your case, you must set the BindingContext property to this:

public CardsListXaml()
{
    InitializeComponent();
    BindingContext = this;
    IsLoading = true;
}

However, this alone will not suffice. Your current solution does not implement a mechanism to notify the view of any property changes, so your view would have to implement INotifyPropertyChanged. Instead, I suggest you implement the Model-View-ViewModel pattern, which not only fits beautifully with data binding, but will result in a more maintainable and testable code base:

public class CardsListViewModel : INotifyPropertyChanged
{
    private bool isLoading;
    public bool IsLoading
    {
        get
        {
            return this.isLoading;
        }

        set
        {
            this.isLoading = value;
            RaisePropertyChanged("IsLoading");
        }
    }

    public CardsListViewModel()
    {
        IsLoading = true;
    }

    //the view will register to this event when the DataContext is set
    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
} 

And then in your code-behind's constructor:

public CardsListView()
{
    InitializeComponent();
    BindingContext = new CardsListViewModel();
}

Just to clarify, DataContext cascades down the visual tree, thus the ActivityIndicator control will be able to read to properties specified in the bindings.

Edit: Xamarin.Forms (and Silverlight/WPF etc... sorry, it's been a while!) also provides a SetBinding method (see the Data Binding section).

like image 133
James Wright Avatar answered Oct 25 '22 12:10

James Wright