4

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.

Ihor Levkivskyi
  • 341
  • 5
  • 14

2 Answers2

3

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.

hvaughan3
  • 10,955
  • 5
  • 56
  • 76
  • 1
    Thanks for response. But BindingContext always is null, when I tried to get var allModel = BindingContext as WallPostViewModel; or var images = (IList)BindingContext; In any cases value and BindingContext are null... – Ihor Levkivskyi Aug 01 '16 at 13:35
  • @IgorLevkivskiy The `CustomVeggieCell.BindingContext` will stay `null` until the `ListView` fills up with `CustomVeggieCell`s and the data is shown on the screen. Try to explain what you are trying to do with `CustomVeggieCell.BindingContext` and maybe we can help you further. – hvaughan3 Aug 01 '16 at 13:44
  • I want to get images from currentItem because each post has different amount photos and that's why I want to display different amount of photos for currentItem using different layouts. – Ihor Levkivskyi Aug 01 '16 at 13:53
  • I want to get bindable property value in constructor in viewcell-class and use this value in some condition in this constructor. On depending condition I want display different layouts( for 1, 2 ,3 images) – Ihor Levkivskyi Aug 01 '16 at 20:31
0
  1. In the constructor of CustomVeggieCell place minimum code. E.g.

    content = new StackLayout(); 
    View = content; 
    

    (assuming you've declared "private StackLayout content" in the fields of CustomVeggieCell)

  2. In this class, override OnBindingContextChanged. At this time, this.BindingContext contains the current item. You can now adapt the content of the base stackLayout according to the data just inserting in the ListView.

adiga
  • 34,372
  • 9
  • 61
  • 83