如何在 Xamarin Forms 中获取 ItemTemplate 中的当前项
How to get current item in ItemTemplate in Xamarin Forms
我为项目 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
中获得 Images
的数量。 Images
只是一个字符串列表。
p.s。 ItemTemplate
中的所有值都是通过绑定获得的。
要获取 ItemTemplate
中的当前项,您只需像这样引用 CustomVeggieCell
的 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<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
绑定到新的可绑定 属性。我再次在文本框中编写了该代码,之前没有测试过任何类似的东西,但如果您 运行 遇到问题,请告诉我。
在 CustomVeggieCell
的构造函数中放置最少的代码。例如
content = new StackLayout();
View = content;
(假设您在 CustomVeggieCell
的字段中声明了 "private StackLayout content")
在此 class 中,覆盖 OnBindingContextChanged
。此时,this.BindingContext
包含当前项。您现在可以根据刚插入 ListView
.
中的数据调整基础 stackLayout 的内容
我为项目 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
中获得 Images
的数量。 Images
只是一个字符串列表。
p.s。 ItemTemplate
中的所有值都是通过绑定获得的。
要获取 ItemTemplate
中的当前项,您只需像这样引用 CustomVeggieCell
的 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<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
绑定到新的可绑定 属性。我再次在文本框中编写了该代码,之前没有测试过任何类似的东西,但如果您 运行 遇到问题,请告诉我。
在
CustomVeggieCell
的构造函数中放置最少的代码。例如content = new StackLayout(); View = content;
(假设您在
CustomVeggieCell
的字段中声明了 "private StackLayout content")在此 class 中,覆盖
OnBindingContextChanged
。此时,this.BindingContext
包含当前项。您现在可以根据刚插入ListView
. 中的数据调整基础 stackLayout 的内容