2016-08-01 52 views
4

我设置模板项目ListView和分配列表项如何获得当前项目中的ItemTemplate在Xamarin窗体

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

如何获得currentItem元素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]")); 
     } 
    } 

我想要在ItemTemplate中获得Images的金额。 Images只是一个字符串列表。

p.s.我通过绑定获得了ItemTemplate中的所有值。

回答

2

要获得一个ItemTemplate你只需要引用CustomVeggieCellBindContext像这样的当前项目:

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

说了这么多,您的代码不完全意义的我。如果您CustomVeggieCell处于ListView,它应该不需要通过硬编码的项目的索引来访问的项目列表像你在这里:

new Binding("Images[1]") 

ListView应该基本上通过所有的项目为做一个foreach您。除非posts包含属性List<string>

编辑:现在我对问题有了更好的理解。您可以在ViewCell上创建新的可绑定属性,并在OnPropertyChanged参数中指定一个方法将图像添加到布局,然后将布局添加到ViewCell。我从来没有尝试过这样的事情,所以这一切都可能根本不起作用。

喜欢的东西:

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 }); 
     } 
    } 
} 

或者类似的东西。然后你会绑定你的posts.Images到你的新的可绑定属性。我再一次在文本框中编写了这个代码,并且之前没有测试过类似的东西,但是如果遇到问题,请告诉我。

+1

感谢您的回复。但是,当我尝试将var allModel = BindingContext作为WallPostViewModel时,BindingContext总是为空;或var images =(IList )BindingContext;在任何情况下,value和BindingContext都为null ... –

+0

@IgorLevkivskiy'CustomVeggieCell.BindingContext'将保持'null'状态,直到'ListView'填充'CustomVeggieCell'并且数据显示在屏幕上。试着解释你想用'CustomVeggieCell.BindingContext'来做什么,也许我们可以进一步帮助你。 – hvaughan3

+0

我想从currentItem中获取图像,因为每个帖子都有不同数量的照片,这就是为什么我想要使用不同布局显示不同数量的照片的currentItem。 –

相关问题