[英]Having trouble binding an Observable collection to my ListView in Xamarin Forms
I am very new to Xamarin Forms and I am trying to bind my Observable collection "DisplayItems" to my listview, but I cannot figure out how to do that and be able to display the name to the item.我对 Xamarin Forms 非常陌生,我正在尝试将我的 Observable 集合“DisplayItems”绑定到我的列表视图,但我不知道如何做到这一点并能够将名称显示到项目。 I can get the make
listview.ItemsSource = DisplayItems
, but when I do I don't know how to make the label in the listview display to get the name of the Item.我可以获得 make
listview.ItemsSource = DisplayItems
,但是当我这样做时,我不知道如何在列表视图中显示 label 以获取项目的名称。 Any help would be appreciated.任何帮助,将不胜感激。
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace ShoppingList
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Page1 : ContentPage
{
public ObservableCollection<Item> DisplayItems;
public Page1()
{
InitializeComponent();
DisplayItems = new ObservableCollection<Item>();
DisplayItems.Add(new Item("potato", 3.40, 3, false));
DisplayItems.Add(new Item("juice", 4.70, 5, true));
BindingContext = this;
}
}
}
XAML: XAML:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="ShoppingList.Page1"
Title ="ViewPage">
<ContentPage.Content>
<StackLayout>
<ListView x:Name="listview" ItemsSource="{Binding DisplayItems}" HasUnevenRows="True" HorizontalOptions="Fill" VerticalOptions="Fill" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell x:Name="viewcell">
<StackLayout>
<Label x:Name="ListCell" Text="{Binding name}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
Having a look at this document about set model data for ListView.查看有关为 ListView 设置 model 数据的文档。 Data Binding in ListView .
ListView 中的数据绑定。
You should define Item class as follow:您应该定义项目class 如下:
public class Item
{
public string name {get; set;}
}
Then in ContentPage add some data DisplayItems :然后在 ContentPage 添加一些数据DisplayItems :
namespace ShoppingList
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Page1 : ContentPage
{
public ObservableCollection<Item> DisplayItems;
public Page1()
{
InitializeComponent();
DisplayItems = new ObservableCollection<Item>();
DisplayItems .Add(new Item{ name="Rob Finnerty"});
DisplayItems .Add(new Item{ name="Bill Wrestler"});
BindingContext = this;
}
}
}
Last in Xaml this can work:最后在 Xaml 这可以工作:
<ListView x:Name="listview" ItemsSource="{Binding DisplayItems}" HasUnevenRows="True" HorizontalOptions="Fill" VerticalOptions="Fill" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell x:Name="viewcell">
<StackLayout>
<Label x:Name="ListCell" Text="{Binding name}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.