Forum Xamarin.Forms

How to make a reusable ViewCell with databinding?

AlexRutherfordAlexRutherford GBUniversity ✭✭
edited June 2017 in Xamarin.Forms

Hey all,

I think im missing something here thats why I am now asking the question. I have an app with a big form that uses the same custom ViewCell I created. I would like to bind the model to the cell for multiple properties but am failing at doing so. I have created a little example below, hope that it helps explain it.

Person Class

    public class Person
        public string Name { get; set; }

        public string LastName { get; set; }

Content Page

        public Person person = new Person();
        public TestView()
            // Set data
            person.Name = "Alex";
            person.LastName = "Bob";

            // Set context
            BindingContext = person;

            // Table
            var section = new TableSection("Test Form");
            var table = new TableView
                RowHeight = 100,
                Root = new TableRoot
                Intent = TableIntent.Form

            // Add a row to table
            var nameCell = new InputCell();

            // Add a row to table
            var lastNameCell = new InputCell();

            // Set the content
            Content = table;

View Cell

    public class InputCell : ViewCell
        public InputCell()
            var stack = new StackLayout
                VerticalOptions = LayoutOptions.FillAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                Padding = 20

            var input = new Entry
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.CenterAndExpand
            input.SetBinding(Entry.TextProperty, new Binding("Name", BindingMode.TwoWay));

            View = stack;

So as you can see I have it bound to just the "Name" of the Person. How can I reuse this cell to work with other properties?

I have attempted to create a bindable property inside ViewCell but can never get it to work at all. Any help would be great. If you need anymore information I will try my best and explain it better, just let me know.

Thanks all!


  • AlexRutherfordAlexRutherford GBUniversity ✭✭

    Bump. I feel like this is just me not understanding how this works, it is really something no one can answer?

  • AlexRutherfordAlexRutherford GBUniversity ✭✭

    I have found a possible answer but unsure if its the correct way to handle it.

    On my main view I no longer set the BindingContext, instead use source: on each of my bindings. Then on my ViewCell I set a BindableProperty and also set the BindingContext = this.

Sign In or Register to comment.