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
});
}
Detail
model – Gerald VersluisBindingContent
before runningInitializeComponent();
. This can prevent an extra layout cycle. – hvaughan3