Access nested lists in Observable Collection for Binding

TrichyTrichy USMember ✭✭

I have response objects that are added to observable collections in my controller like below. All response objects are contained in their own observable collection as well as being contained within the main ScheduleRoute object.

            public void AddRoutes(IRestResponse response)
            {
                ScheduleDownloadResponse json = JsonConvert.DeserializeObject<ScheduleDownloadResponse>(response.Content);

                ScreenController.Instance.route = new ObservableCollection<ScheduleRoute>();
                ScreenController.Instance.vehicle = new ObservableCollection<Vehicle>();

                ScreenController.Instance.pickups = new ObservableCollection<Pickup>();
                ScreenController.Instance.compartment = new ObservableCollection<Compartment>();

                foreach (var item in json.Routes)
                {
                    Debug.WriteLine(json.Routes);
                    ScreenController.Instance.route.Add(item);


                    foreach(var vehicle in item.Vehicles)
                    {
                        ScreenController.Instance.vehicle.Add(vehicle);

                        foreach (var compartment in vehicle.Compartments)
                        {
                            ScreenController.Instance.compartment.Add(compartment);
                        }

                    }

Inside the ScheduleRoute is other response objects eg. Vehicles contain the Compartment object. Pickup contains the Supplier Object.

            "Routes": [
            {
            "Id": 1,
            "Date": "2015-03-04T00:00:00",
            "Area": "2887",
            "Detail": "",
            "Driver": "",
            "SampleType": "",
            "Schedule": "A",
            "Tag": "",
            "Vehicles": [
                  {
                    "VehicleId": 22,
                    "VehicleRego": "RRK160",
                    "Primary": true,
                            "Compartments": [
                              {
                                "Id": "1",
                                "Capacity": 8000.0,
                                "Product": ""
                              }
                    ]
                  },
            "Pickups": [
                  {
                    "Quantity": 6300.0,
                    "Id": 1,
                    "Vat": 0,
                    "Hazards": "",
                    "Latitude": "-34.33000000000",
                    "Longitude": "-63.29119000000",
                    "Message": "",
                             "Supplier": {
                              "Id": 1134,
                               "Name": "GUTIERREZ REMO O. Y RODOLFO S."
                    },
                    "Product": "Milk",
                    "ProductKey": "2",
                    "Ticket": "",
                    "EstmatedPickup": "2015-03-04T20:00:00+13:00"
                  },

In the view I need to display the route id,date,schedule, area and the VehicleRego from Vehicles and the Id from Supplier.

In my View model I have a Schedule observable collection that is a copy of the original ScheduleRoute collection from my Screen controller

    Schedule = new ObservableCollection<ScheduleRoute>();
    Schedule = ScreenController.Instance.route; 

I am binding to Schedule in my view and can display all first level attributes in route but dont know how to get to Vehicles.VehicleRego and Vehicles.Suppliers.Id

Im not sure how to either access the nested levels from scheduleRoute or combine 3 collection objects(ScheduleRoute, Vehicle and Supplier) to retrieve values

I am very new to Xamarin and C#.

Sign In or Register to comment.