0
votes

I am having a problem binding object properties to labels. In the example below, the object is retrieved and set as the BindingContext as per the documentation.

The issue is that when testing, the BusinessName label displays the value as expected but the PostCode label remains empty. To test that the property does actually contain a value I have set the 3rd label text which displays correctly.

Please let me know if more information is required.

public partial class AppointmentPage : ContentPage
{
    public AppointmentPage(int calendarid)
    {
        InitializeComponent();
        Detail d = JsonConvert.DeserializeObject<Detail>Core.GetDetail(calendarid).ToString());

        BindingContext = d;
        test.Text = d.PostCode;
    }
}

XAML

<?xml version="1.0" encoding="utf-8" ?>
   <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
   xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
   x:Class="App.AppointmentPage">
<ContentPage.Content>
<ScrollView>
  <StackLayout>
            <Label Text="{Binding BusinessName}" />
            <Label Text="{Binding PostCode}" />
            <Label x:Name="test" Text="" />
</StackLayout>
</ScrollView>
</ContentPage.Content>
</ContentPage>

Detail Model

class Detail
{
    public int CalendarID { get; set; }
    public int RecordID { get; set; }
    public DateTime StartDateTime { get; set; }
    public DateTime EndDateTime { get; set; }
    public string BusinessName { get; set; }
    public string Address { get; set; }
    public string PostCode { get; set; }
    public string WorkTel { get; set; }
    public string Email { get; set; }
    public string Status { get; set; }
    public int RepID { get; set; }
}

UPDATE

The page that this is linked from runs the following code when the item is selected from a list. The problem is being caused where I set the BindingContext to the SelectedItem (which contains a BusinessName but not a PostCode) and this is overwriting what is being set on the page load.

async void OnListItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        await Navigation.PushAsync(new AppointmentPage((e.SelectedItem as Diary).CalendarID)
        {
            BindingContext = e.SelectedItem as Diary

        });
    }
1
Please show the code of your Detail modelGerald Versluis
I have just edited the question to include model code.Xtravia9
Looks like everything is correct. Do you see any errors about bindings or XAML stuff in the output window when navigating to that page while debugging? Also, try setting the BindingContent before running InitializeComponent();. This can prevent an extra layout cycle.hvaughan3
Check if the json file contains a postcode, maybe you getting a null one, also check if you're matching correctly the model name with the server name, once happens to me that i set the name as PostCode and from server side the name was ZipCode.FabriBertani
@hvaughan3 I'm not seeing anything unusual in the the Output window. Thanks for the InitializeComponent() tip.Xtravia9

1 Answers

0
votes

Your code seems to be ok. Here is my test, I can only assume that there is a problem with GetDetails()

CodeBehind

public Page1 ()
{
    InitializeComponent ();
    var d = new Detail {
        BusinessName = "Name",
        PostCode = "123"
    };
    BindingContext = d;
    test.Text = d.PostCode;
}

enter image description here