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

What the....? Is it not possible to filter an ItemsSource directly on a view's binding?

$
0
0

Hello, I've got quite a complicated app where I need to build out all of the form fields dynamically from a database, so I won't know ahead of time how many of certain types of controls there will be before rendering them to the page.

An example of this is, I could have more than one place on a page where users can click to upload images, whereby they are shown the uploaded images as thumbnails afterwards. This kind of field is mixed in with text fields, switches, etc.

Due to the dynamic nature of everything, I have a single ObservableCollection that contains image information that they uploaded/added to the record. My idea was to add a filter to each of these image uploader's gallery of thumbnails to only show uploaded images for that particular field. Something like this:

list.ItemsSource = ViewModel.Pictures.Where(p => p.SurveyColumnId == question.Id);

Now, the SurveyColumnId of the object is set when that picture is added to the collection. The SurveyColumnId and question.Id values should match in this case, and they really do.

The problem is, no items appear to be bound when the Where clause is added. If I take off the Where clause, they show up, but they show up everywhere on the page for this form field. Here's a full example for context:

else if (question.DataType == DataType.Photo) {
                // New horizontal stack layout for keeping the selected images next to each other.
                var stack = new StackLayout { Orientation = StackOrientation.Horizontal };

                var button = new Button {
                    Text = "Take picture",
                    StyleId = question.Id
                };
                button.Command = ViewModel.Choice;
                button.CommandParameter = question.Id;

                var list = new ListView ();

                //Bind the items in our list to the observable collection of pictures for this question.
                list.ItemsSource = ViewModel.Pictures.Where(p => p.SurveyColumnId == question.Id);
                //list.ItemsSource = ViewModel.Pictures;
                var cell = new DataTemplate(typeof(ImageCell));
                cell.SetBinding(ImageCell.ImageSourceProperty, "Path");
                list.ItemTemplate = cell;
                list.HeightRequest = 80;
                list.VerticalOptions = LayoutOptions.FillAndExpand;

                stack.Children.Add (button);
                stack.Children.Add (list);
                return stack;
            }

Any ideas why this Where clause isn't working? I checked the item values and everything checks out.

Thanks!


Viewing all articles
Browse latest Browse all 58056

Trending Articles



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