Forum Xamarin.Mac


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.

How to implement 2 way data binding to a textfield

blagovestblagovest Member ✭✭

Hi team,

I implement a field which input is "sanitised" via regex and if the input has chars that are not allowed by the regex the value of the exported property should not change. I made the the field to update the property on every input character. The value of the exported property updates as expected (checked via debugging) but the UI text control doesn't update the value and the wrong character stays in it. As per different articles I saw that the KVO/KVC pattern doesn't include in itself 2 way data binding. How I should circumvent that?


  • XamarinProblemsXamarinProblems Member ✭✭✭
    edited August 2019

    Are you Two way data binding to send the text to the viewModel to handle the regex alteration of the string?

    You shouldn't have to make a two way data binding, your input has a TextChanged property, go into your xaml.cs class where your input is and make a Text changed event

     private void SomeEntryName_TextChanged(object sender, TextChangedEventArgs e)
            var entry = (Entry)sender;
                 if (entry.Text != null)
                     if (entry.Text.Length == 10) //Random example of something you could do could also check for containing stuff.
                         entry.Text = e.OldTextValue;                  

    Go to your xaml class where you created your entry and add the name with x:name = "someEntryName"

     <Entry x:Name="someEntryName" Text="{Binding TextValue}"/> 

    Then in the constructor of your xaml.cs class access the entry with the name and add the behavior to the entry
    someEntryName.TextChanged += SomeEntryName_TextChanged;

    I hope this helps you

Sign In or Register to comment.