listviewxamarin.formsitemtemplate

How to get current item in ItemTemplate in Xamarin Forms


I set template for item ListView and assign list items

listView.ItemTemplate = new DataTemplate(typeof(CustomVeggieCell));  
listView.ItemsSource = posts;

How to get currentItem element in CustomVeggieCell:

CustomVeggieCell.class:

    public class CustomVeggieCell : ViewCell
   {
          public CustomVeggieCell(){
        // for example int count = currentItem.Images.Count;
        var postImage = new Image
        {
            Aspect = Aspect.AspectFill,
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand
        };
        postImage.SetBinding(Image.SourceProperty, new Binding("Images[0]"));
        var postImage = new Image
        {
            Aspect = Aspect.AspectFill,
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand
        };
        postImage.SetBinding(Image.SourceProperty, new Binding("Images[1]"));
        }
    }

I want to get amount of Images in ItemTemplate. Images is simply a list of strings.

p.s. All value in ItemTemplate I get by binding.


Solution

  • To get the current item in an ItemTemplate you only need to reference the CustomVeggieCell's BindContext like so:

    string imageString = (string)BindingContext; //Instead of string, you would put what ever type of object is in your 'posts' collection
    

    Having said that, your code does not completely make sense to me. If your CustomVeggieCell is in a ListView, it should not need to access a list of items by hardcoding the index of the item like you have here:

    new Binding("Images[1]")
    

    The ListView should basically do a foreach through all of the items for you. Unless posts contains a property which is List<string>.

    Edit: Now that I have a better understanding of the issue. You may be able to create a new bindable property on your ViewCell and specify a method in the OnPropertyChanged param to add the images to a layout and then add the layout to your ViewCell. I have never tried anything like this so be that this all might not work at all.

    Something like:

    public class CustomVeggieCell : ViewCell
    {
        public List<ImageSource> Images {
            get { return (ImageSource)GetValue(ImagesProperty); }
            set { SetValue(ImagesProperty, value); }
        }
    
        public static readonly BindableProperty ImagesProperty = BindableProperty.Create("Images", typeof(List<ImageSource>), typeof(CustomVeggieCell), null, BindingMode.TwoWay, null, ImageCollectionChanged);
    
        private StackLayout _rootStack;
    
        public InputFieldContentView() {
            _rootStack = new StackLayout {
                Children = //Some other controls here if needed
            };
    
            View = _rootStack;
        }
    
        private static void ImageCollectionChanged(BindableObject bindable, object oldValue, object newValue) {
            List<ImageSource> imageCollection = (List<ImageSource>)newValue;
    
            foreach(ImageSource imageSource in imageCollection) {
                (CustomVeggieCell)bindable)._rootStack.Children.Add(new Image { Source = imageSource });
            }
        }
    }
    

    Or something like that. Then you would bind your posts.Images to your new bindable property. Again I wrote that code just now in the text box and have not tested anything like that before, but let me know if you run into problems.