Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

How should I connect property changes via bindings in Android views when using a custom renderer?

$
0
0

I've been trying to write a simple textarea control, which would behave sort of like the HTML <textarea>. Currently I'm trying to figure out what is the best way to bind the data from the Android view back to my Xamarin Froms view. Here's how my renderer looks

class TextareaEntryRenderer : ViewRenderer<TextareaEntry, TextView>
{
    private EditText _control;

    protected override void OnElementChanged(ElementChangedEventArgs<TextareaEntry> e)
    {
        base.OnElementChanged(e);
        _control = new EditText(this.Context)
        {
            Text = Element.Text
        };

        SetNativeControl(_control);
    }

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);

        if (e.PropertyName == Entry.TextProperty.PropertyName)
        {
            System.Diagnostics.Debug.WriteLine("Changed text property: {0}", Element.Text);
            _control.Text = Element.Text;
        }
        else
        {
            System.Diagnostics.Debug.WriteLine("OnElementPropertyChanged: {0}", e.PropertyName);
        }
    }
}

The only thing that seems to make sense is to bind to the TextChanged event on the _control, such as _control.TextChanged += (sender, args) => Element.Text = _control.Text;, but this seems to be creating a loop with the OnElementPropertyChanged going the othwer way around.

Is there a way to connect the two properties together into a single binding?


Viewing all articles
Browse latest Browse all 58056

Trending Articles