Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Bindings, bindings, bindings - Its doin' my 'ead in!!

DaveMartinDaveMartin NLMember ✭✭
edited March 2018 in Xamarin.Forms

Ive taken a piece of code from the forum and worked it into my project:

var dirName = new Entry()
                Text = "",
                BackgroundColor = Color.White,
                TextColor = Color.Black,
                HeightRequest = 40,
                WidthRequest = 140,
                HorizontalOptions = LayoutOptions.FillAndExpand
 string item = "foo";
 dirName.BindingContext = item;
 dirName.SetBinding(Entry.TextProperty, ".", BindingMode.TwoWay);

Now, although the string 'foo' appears in the entry 'dirname' when I open the form, changes to that entry do not update the item variable which is really what Im looking for.

Why is that? Any help is greatly appreciated..!


  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    What is "item"? Do you implement INPC?

  • DaveMartinDaveMartin NLMember ✭✭

    No Idea what INPC is. I just picked this example up from this forum to see if it would solve my problem, that of Twoway binding.

  • JohnHardmanJohnHardman GBUniversity admin
    edited March 2018

    @DaveMartin - You will probably get further if you separate your View and View Model.

    Create a ViewModel class, and add a string property (i.e. something with get and set) to it (this replaces what you currently have as item)
    Your BindingContext will want to reference an instance of that class.
    The Binding will then use nameof(the string property) (this replaces what you currently have as ".")

    That's not to say you couldn't do it all in one class. If you really want to do that, replace item by a string property, set BindingContext to this, and use nameof(the string property)

    In both scenarios, to make the code useful, you will need to raise a PropertyChanged event when the string property changes (I've never seen it abbreviated to INPC, but that will be the INotifyPropertyChanged interface, an interface that contains just the event PropertyChangedEventHandler PropertyChanged)

  • DaveMartinDaveMartin NLMember ✭✭


    Hi John, I can carry out most of that, but how do I use a PropertyChanged Event? I dont want to get too complex, as Im just beginning with Xamarin.

    I need really just to pick up changes to a form when the user presses the Save Button. All the Entry.Text properties.

  • JohnHardmanJohnHardman GBUniversity admin
    edited March 2018

    @DaveMartin - See the Xamarin.Sport sample, particularly the file at

    Have your ViewModel class inherit from BaseNotify, then in your string property's setter, if value != the current value, call SetPropertyChanged

  • DaveMartinDaveMartin NLMember ✭✭
    edited April 2018

    @John Hardman,

    Hi John,
    I still dont quite understand why it doesnt actually work. Seems to me the point of binding variables together must mean that you dont need to write event handlers to copy values from variables to fields and vice versa. Or am I missing something?

    I dont want to get into MVVM at the moment, I just need to fix what I have or at least understand why it doesn't work, without throwing the baby and the bath water out...

  • JohnHardmanJohnHardman GBUniversity admin

    @DaveMartin - I think it's easier to understand if you do actually separate the View and View Model, even just for a short learning exercise.

    The PropertyChanged notification is required so that the View (in your case the Entry) knows to update itself using the binding. Without that, it wouldn't know that the underlying data has changed.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    PropertyChanged Fody save your life
  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @DaveMartin said:
    I dont want to get into MVVM at the moment, I just need to fix what I have

    I read that a lot here. You may not like frankness and hopefully you're not offended but... You should have learned MVVM at the start and built the application correct from day one. Every step you take after starting out even 2° off course just takes you further and further from reaching your destination. You're basically saying "I don't want to learn about how to put gas in the car. I just want to do the basics of driving for now and worry about the extra stuff later". Only you can't drive because you don't have fuel. It really doesn't matter if you do or don't want to learn about foundation concepts. You really need to understand and use these principle if you want to code in this eco-system. You may not like it, or want it, but you need to stop fighting it and stop digging in your heels. Take a deep breath. Relax. And start a new project correctly architected around the MVVM pattern. At first you'll feel like you're loosing time. But when you can move further faster without spending tens of hours working around bad design you'll see you actually save time by starting over. Besides, huge parts of your existing code can probably be brought in by either importing entire files, or copy/pasting from the old project to the new. If the old solution is really half decent there isn't that much loss. If there is a lot of loss, then it wasn't half decent and you needed to start fresh anyway.

  • DaveMartinDaveMartin NLMember ✭✭
    Is the event raised automatically when the user types into the form?
  • JohnHardmanJohnHardman GBUniversity admin

    @DaveMartin - Give it a go and see what happens :-) It's the best way to learn.

    A ContentPage with four Entry views, one using OneWay, one using OneWayToSource, one using TwoWay, and one using Default, each binding to a different string property in the View Model. Also add a Button that when pressed changes the string properties in the View Model.

    That should only take a few minutes to implement, then you can put breakpoints in using the IDE and see what happens (or use trace statements in the form of System.Diagnostics.Debug.WriteLine). Type into the Entry views to see what happens, and press the Button to see what happens.

  • DaveMartinDaveMartin NLMember ✭✭
    I cant really refuse anymore, I guess... :smile:
Sign In or Register to comment.