0
votes

In addition to the Microsoft docs, I've been referencing this SO question: Xamarin Forms Databinding "." separator

The 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"
    xmlns:d="http://xamarin.com/schemas/2014/forms/design"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:local="clr-namespace:NAS.App.Framework"
    x:Class="NAS.App.Pages.Complications"
    ControlTemplate="{StaticResource ContentPageTemplate}">
    <ContentPage.Content>
        <StackLayout>
            <CheckBox IsChecked="{Binding InnerProperty1.NestedValue}" />
            <CheckBox IsChecked="{Binding InnerProperty1Checked}" />
            <Label Text="{Binding InnerProperty1.NestedLabel}" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

The code behind:

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace NAS.App.Pages
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Complications : ContentPage
    {
        public Complications()
        {
            InitializeComponent();
            InnerProperty1 = new NestedProperty("InnerProperty1");

            BindingContext = this;
        }

        public bool InnerProperty1Checked
        {
            get { return InnerProperty1.NestedValue; }
            set { InnerProperty1.NestedValue = value; }
        }

        public NestedProperty InnerProperty1;
    }

    public class NestedProperty
    {
        public NestedProperty(string label)
        {
            NestedLabel = label;
        }

        private bool _value;
        public bool NestedValue
        {
            get { return _value; }
            set
            {
                _value = value;
            }
        }

        public string NestedLabel { get; private set; }
    }
}

Based on MS docs and the referenced article, I would expect the Label element, bound to InnerProperty1.NestedLabel to display the text "InnerProperty1" (as passed to the NestedProperty constructor). The Label element displays nothing - the binding isn't working.

I would also expect the NestedProperty.NestedValue's setter to get called when I check or uncheck the first Checkbox element. A breakpoint on the "_value = value;" line is not hit. The binding isn't working here, either.

I wrap the InnerProperty with a direct property, InnerPropert1Checked, and bind the second Checkbox element to that. The breakpoint is hit when I check / uncheck the second Checkbox. The binding to the non-nested property does work.

I can't come up with an explanation as to why binding to the nested properties isn't working.

Xamarin Forms 4.6.0.726

Also, the page is the ContentPresenter portion of a ControlTemplate that's defined in App.xaml. And the page is a ShellContent element in AppShell.xaml. Given that non-nested binding is working, I'm not sure how either of those would come into play. But maybe I'm missing something there.

Any thoughts? Thank you.

1
you can only bind to public properties. InnerProperty1 is not a property as it does not have a getJason

1 Answers

4
votes

you can only bind to public properties

InnerProperty1 is a field, not a property

public NestedProperty InnerProperty1;

add a get to make it a property

public NestedProperty InnerProperty1 { get; set; }