Search code examples
c#data-bindingxamarin.forms

Unable to bind properties of an object to a custom control


I have a content page which is bound to a view model.

In the view model i new up an address and give it some data

Address = new Address
{
     UserId = Guid.NewGuid(),
     PrimaryAddress = true,
     Postcode = "G23 5HU",
     Street = "Smith Street",
     City = "Large City",
     PhoneNumber = "115151"
};

I also have an AddressInfo control on the content page like so

 <controls:AddressInfo Address="{Binding Address}"></controls:AddressInfo>

Here is the control it's self

public partial class AddressInfo : ContentView
{
    public static readonly BindableProperty AddressProperty =
        BindableProperty.Create(nameof(Address), typeof(Address), typeof(AddressInfo), null);

    public Address Address
    {
        get { return (Address)GetValue(AddressProperty); }
        set { SetValue(AddressProperty, value); }
    }

    public AddressInfo()
    {
        InitializeComponent();
        Street.SetBinding(Label.TextProperty, new Binding(nameof(Address.Street), source: this));
        City.SetBinding(Label.TextProperty, new Binding(nameof(Address), source: this));
        PhoneNumber.SetBinding(Label.TextProperty, new Binding(nameof(Address), source: this));
        PostCode.SetBinding(Label.TextProperty, new Binding(nameof(Address), source: this));
    }

Here is the control XAML

<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="TyreKlicker.XF.Core.Controls.AddressInfo">
<ContentView.Content>

    <StackLayout>
        <Label
                x:Name="Street"
                Text="{Binding Address.Street}" />
        <Label
                x:Name="City"
                Text="{Binding Address.City}" />
        <Label  Text="Hardcoded Text" ></Label>
        <Label
                x:Name="PostCode"
                Text="{Binding Address.Postcode}" />
        <Label
                x:Name="PhoneNumber"
                Text="{Binding Address.PhoneNumber}" />

        <Label x:Name="CardHeader"
                   Text="{Binding Header}"
                   TextColor="{StaticResource PrimaryDark}"
                   FontSize="Large" Margin="10,0,0,0" />

        <Button Clicked="Button_OnClicked"></Button>
    </StackLayout>
</ContentView.Content>

When i run it i would expect to see the Address properties but instead its all blank except the Hardcoded Text

Picture of the app running

I added a button to the control to assist with debugging if i put a stop on the Button_OnClicked event i can see the properties of the Address and it has the correct data but it just not showing it on the list what am i doing wrong?

Address properties


Solution

  • Thanks to the hints from Jason i removed the following lines from the AddressInfo constructor and viola it worked

            //Street.SetBinding(Label.TextProperty, new Binding(nameof(Address.Street), source: this));
            //City.SetBinding(Label.TextProperty, new Binding(nameof(Address), source: this));
            //PhoneNumber.SetBinding(Label.TextProperty, new Binding(nameof(Address), source: this));
            //PostCode.SetBinding(Label.TextProperty, new Binding(nameof(Address), source: this));
    

    Thanks for the help!