Binding an array or list inside a custom ViewCell

KrahosKrahos ITMember

Hello everyone, in my View I have a ListView populated by binding its ItemsSource to a List of objects (in my ViewModel), each of these objects contains a List of boolean values, the number of booleans is inserted by the user at runtime. I wanted to know if there is a way to bind the List of booleans of each object in my ViewModel to the List of Switches In my View (or rather, binding each bool to each Switch) without breaking the MVVM pattern. Also in general what's the correct way to handle a ListView which contains a custom ViewCell with an array or list of items to display.

The DataTemplate for the ListView is crated at runtime as such:
`

            MyListView.ItemTemplate = new DataTemplate (() => {
                MyCustomCell tempcell = new MyCustomCell (numberOfBooleans);
                Switch tempSwitch;
                for (int i = 0; i < numberOfBooleans; ++i) {
                    tempSwitch = new Switch {
                        HorizontalOptions = LayoutOptions.CenterAndExpand
                    };

                    tempcell.ContainingLayout.Children.Add (tempSwitch);
                    tempcell.SwitchList.Add (tempSwitch);
                }
                return tempcell;
            });

`

Basically what i want to do is to let the user switch at will from true to false and vice versa every element of a list of bool [].

Thanks all.

Answers

  • N_BauaN_Baua INMember ✭✭✭✭✭

    Hi @Krahos ,

    I am not sure, if you've already gone through this docs and this meets your requirements, though please have a look at it.

    Hope it helps.

    -- N Baua

  • KrahosKrahos ITMember
    Hi, yes I had a look at this. Although I need a variable number of switches for each cell. And in general I wanted to know how to handle a variable number of elements (they could be labels or entries) inside a custom ViewCell.
  • N_BauaN_Baua INMember ✭✭✭✭✭

    Well may be you can create a template/control which parses your data (JSON/string) and loops through it rendering the required control set. I generally do not advocate using the code behind model, however in this particular case you might want to use the same.

Sign In or Register to comment.