Forum Xamarin.Forms

How can I bind a dynamic data model to a custom listview cell?

KarenCateKarenCate USMember ✭✭

Hopefully I'm using the correct terms in that question.

I am trying to replicate a very flexible data presentation model that we use in our desktop applications. We have a proprietary data server, and a scripting language to access it. The server returns a header with the data that defines how many fields comprise a row, and the names and types for each field. Our desktop application can then configure the table/grid/spreadsheet/(whatever you want to call a dataset represented in rows and columns) it presents to the user on the fly. This allows us to customize the data each customer sees without having to create custom applications.

Any hints on how I might accomplish this? I was thinking of using a listview, and building the cell at runtime, but I can't figure out how to get the data into the created cells. All the examples I've found that have created the cell layout at runtime are still based on a data model with static properties that can be bound. I essentially need to bind to an array index or element.

I want to do something like this (and I'm sure it's riddled with errors):

class AResult
    public short[] FieldSizes;
    public short[] FieldTypes;
    public string[] FieldNames;
    public string[][] FieldValues;

    public short FieldCount;
    public short RowCount;

    AResult(string query)
            ... do stuff...

public void OnQueryButtonClicked(object sender, EventArgs args)
    AResult result = new AResult("some query");
    var newtemplate = new DataTemplate(() =>
        var newview = new ViewCell {
            View = new StackLayout {
                Orientation = StackOrientation.Horizontal
        return newview;
    for (int i=0; i<result.FieldCount; i++)
        var lbl = new Label();
        lbl.BindingContext = i; // <-- Yes I know this doesn't work...
        newtemplate.View.Children.Add(lbl);  // Or this either...

But that totally doesn't work.

If you know of any example code that does something even remotely like this, I'd really appreciate a pointer to it. Or any other hints or recommendations.


Sign In or Register to comment.