我设置模板项目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
中的所有值。
感谢您的回复。但是,当我尝试将var allModel = BindingContext作为WallPostViewModel时,BindingContext总是为空;或var images =(IList)BindingContext;在任何情况下,value和BindingContext都为null ... –
@IgorLevkivskiy'CustomVeggieCell.BindingContext'将保持'null'状态,直到'ListView'填充'CustomVeggieCell'并且数据显示在屏幕上。试着解释你想用'CustomVeggieCell.BindingContext'来做什么,也许我们可以进一步帮助你。 – hvaughan3
我想从currentItem中获取图像,因为每个帖子都有不同数量的照片,这就是为什么我想要使用不同布局显示不同数量的照片的currentItem。 –