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

Data binding in my custom control

$
0
0

I want to create my custom cell to basically replace

<ViewCell Height="300">
    <Editor Text="{Binding Template}" HeightRequest="300" />
</ViewCell>

by

<cells:EditorCell Text="{Binding Template}" Height="300" />

So I wrote the simple EditorCell like this:

public class EditorCell : ViewCell
{
    public static readonly BindableProperty TextProperty = BindableProperty.Create<EditorCell, string>(p => p.Text, null, BindingMode.TwoWay);

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public EditorCell()
    {
        var editor = new Editor();
        editor.SetBinding(Editor.TextProperty, (EditorCell ec) => ec.Text, BindingMode.TwoWay);
        editor.SetBinding(Editor.HeightRequestProperty, (EditorCell ec) => ec.Height, BindingMode.OneWay);

        View = editor;
    }
}

The Height property of my EditorCell is bound to the HeightRequest of the Editor as it should be, but the Text always remains null.

I set breakpoints in the constructor of EditorCell and in the getter and setter of the Text property. The constructor is hit, but the getter and setter are not. Neither during initial databinding nor later when I enter text in the Editor.
I checked that my viewmodel really contains text in its Template property and this text is used when I use the old xaml with the ViewCell.
If I deliberately misspell the Text property in my xaml, then I get an exception. So it should really use the correct property.

What am I doing wrong?
Could it be a problem that I bind the Text property to my viewmodel and at the same time to the Editor.TextProperty?


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>