Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why can't I use Text="{Binding City, Source={Binding Address}}"?

Given two models:

public class Person
{
    public Address Address { get; set; } = null!;
}
public class Address
{
    public string City { get; set; } = null!;
}

And a page:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        BindingContext = new Person
        {
            Address = new Address { City = "New York" }
        };
    }
}
<Label Text="{Binding}" BindingContext="{Binding Address.City}" />

The above code works as expected but why can’t it be rewritten as follows?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<Label Text="{Binding City, Source={Binding Address}}" />
<Label Text="{Binding}" BindingContext="{Binding City, Source={Binding Address}}" />

>Solution :

why can’t it be rewritten as follows?

Binding sources aren’t bindable, it’s just how it works. However, there’s a much simpler way to write it correctly:

<Label Text="{Binding Address.City}" />
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading