Forum Xamarin.Forms


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Binding to nested object (xaml -> ContentPage -> Object)


I'm learning how Xamarin works, and need help to understand how to make the binding on XAML to an object that is a property of my ContentPage:


<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns=""

        HorizontalOptions="FillAndExpand" Padding="20">

        <Label Text="Iniciar com um bloco em branco aberto?" 
               Grid.Row="0" Grid.Column="0"
               IsToggled="{Binding conf.abrirBlocoBranco, Mode=TwoWay}"
               Grid.Row="0" Grid.Column="1"

        <Label Text="Idioma" 
               Grid.Row="1" Grid.Column="0"
        <Label Text="Português" 
               Grid.Row="1" Grid.Column="1"



C# ContentPage

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

namespace Campo_Minado
    public partial class ConfigPage : ContentPage

        protected Config conf { get; set; } = Config.obj;

        public ConfigPage ()
            InitializeComponent ();

        protected override void OnDisappearing()

C# Object

using System;
using System.Collections.Generic;
using System.Text;

using Xamarin.Forms;

namespace Campo_Minado
    public class Config
        public static Config obj = new Config();

        public Config()


         *  Propriedades de vibração e som:
         *  1 - vibração (0x0001)
         *  2 - som (0x0010)
         *  3 - vibração e som (0x0011)

        public byte tapHold { set; get; } = 1;

        public bool abrirBlocoBranco { set; get; } = true;

        public String idioma { set; get; } = "por";

        public void carregar()
            IDictionary<String,Object> props = Application.Current.Properties;

            if (props.ContainsKey("tapHold")) tapHold = (byte)props["tapHold"];
            if (props.ContainsKey("abrirBlocoBranco")) abrirBlocoBranco = (Boolean)props["abrirBlocoBranco"];
            if (props.ContainsKey("idioma")) idioma = (props["idioma"] as string)?? "por";
        public void salvar()
            IDictionary<String, Object> props = Application.Current.Properties;

            props["tapHold"] = tapHold;
            props["abrirBlocoBranco"] = abrirBlocoBranco;
            props["idioma"] = idioma;

            Console.WriteLine("Gravando informações: abrirBlocoBranco = {0}", abrirBlocoBranco);

What i'm trying to do: use the app and have the "conf.abrirBlocoBranco" set to the value of the switch.

I also try:

        <Switch BindingContext="{x:Reference conf}"
               IsToggled="{Binding abrirBlocoBranco, Mode=TwoWay}"
               Grid.Row="0" Grid.Column="1"

but with this i receive the error: "Can't resolve name on Element".

The log (on "salvar... Console.WriteLine(...)") is always "true" for the value (the default that i set lines above).

Thanks for your attention guys!


Best Answer


  • rcherutircheruti BRMember

    Thanks guys for your help!

    @Hunuman , this is exatly (part) of what i need!

    i have added on XAML:

    x:Name="page"  // to ContentPage tag
    BindingContext="{x:Reference page}"  //  to Grid tag
    IsToggled="{Binding Path=conf.abrirBlocoBranco}"  //  to Switch tag

    Also is important to note that all that will only work if the property on C# is public, i declared it protected first time and it was not working until i changed it.

    @ClintStLaurent , thanks for the article, there is too a case where was used the same strategy (the "x:Name"). I will study more about that custom tags/components.

    I mark @Hunuman because the answer was closest to the solution, more direct to the solution.

    Thanks guys!

Sign In or Register to comment.