Listview 如何在Xamarin表单中获取ItemTemplate中的当前项
我为itemListview 如何在Xamarin表单中获取ItemTemplate中的当前项,listview,xamarin.forms,itemtemplate,Listview,Xamarin.forms,Itemtemplate,我为itemListView设置模板并分配列表项 listView.ItemTemplate = new DataTemplate(typeof(CustomVeggieCell)); listView.ItemsSource = posts; 如何在CustomVeggieCell中获取currentItem元素: CustomVeggieCell.class: public class CustomVeggieCell : ViewCell { pub
ListView
设置模板并分配列表项
listView.ItemTemplate = new DataTemplate(typeof(CustomVeggieCell));
listView.ItemsSource = posts;
如何在CustomVeggieCell
中获取currentItem
元素:
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
中获取图像的数量<代码>图像
只是一个字符串列表
p、 s.
ItemTemplate
中的所有值都是通过绑定获得的 要在ItemTemplate
中获取当前项,只需引用customvegiecell
的BindContext
,如下所示:
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
属性
编辑:现在我对这个问题有了更好的理解。您可以在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 });
}
}
}
公共类CustomVeggieCell:ViewCell
{
公共列表图像{
获取{return(ImageSource)GetValue(ImagesProperty);}
set{SetValue(ImagesProperty,value);}
}
public static readonly BindableProperty ImagesProperty=BindableProperty.Create(“图像”、typeof(列表)、typeof(CustomVeggieCell)、null、BindingMode.TwoWay、null、ImageCollectionChanged);
私有StackLayout\u rootStack;
公共InputFieldContentView(){
_rootStack=新的StackLayout{
Children=//如果需要,这里还有一些其他控件
};
视图=_根堆栈;
}
私有静态void ImageCollectionChanged(BindableObject bindable、object oldValue、object newValue){
列表imageCollection=(列表)newValue;
foreach(imageCollection中的ImageSource ImageSource){
(CustomVeggieCell)可绑定);
}
}
}
或者类似的。然后将posts.Images
绑定到新的可绑定属性。同样,我刚才在文本框中编写了代码,以前没有测试过类似的东西,但是如果遇到问题,请告诉我
customvegiecell
的构造函数中放置最小代码。例如
content = new StackLayout();
View = content;
(假设您在CustomVeggieCell
的字段中声明了“私有StackLayout内容”)this.BindingContext
包含当前项。现在,您可以根据刚刚插入到列表视图中的数据调整基本堆栈布局的内容
谢谢你的回复。但当我试图将var allModel=BindingContext作为WallPostViewModel获取时,BindingContext总是为null;或var images=(IList)BindingContext;在任何情况下,value和BindingContext都为null…@IgorLevkivskiy
CustomVeggieCell。BindingContext
将保持null
,直到ListView
填满CustomVeggieCell
并在屏幕上显示数据。试着解释一下你想用CustomVeggieCell.BindingContext做什么,也许我们可以进一步帮助你。我想从currentItem获取图像,因为每篇文章都有不同数量的照片,这就是为什么我想使用不同的布局为currentItem显示不同数量的照片。我想在中获取可绑定属性值并在此构造函数中的某些条件下使用此值。根据不同的条件,我希望显示不同的布局(对于1、2、3个图像)