我在尝试简单的事情,例如设置按钮的列表视图并将这些按钮的文本绑定到列表中的元素。绑定按钮文本到ObservableCollection
这里是测试MainPage。
public MainPage()
{
InitializeComponent();
ObservableCollection<Lot> lotList = new ObservableCollection<Lot>();
lotList.Add(new Lot(1));
lotList.Add(new Lot(2));
lotList.Add(new Lot(3));
lotList.Add(new Lot(4));
lotList.Add(new Lot(5));
this.BindingContext = lotList;
}
这是相当直接的,创建项目列表并添加项目到它。
的XAML看起来是这样的:
<ListView x:Name="lotListView" HorizontalOptions="Center" ItemsSource="{Binding}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Button Text="{Binding}"/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
这是做我猜常见的事?
我确实显示了正确数量的按钮,但没有写入它们。 我的问题是:我如何使按钮的Text属性从列表中的项目获取名称? 我以为ToString方法会被调用,但看起来并不如此。
的很多课,根据要求:
public class Lot
{
private int id { get; set; }
public String Name { get; set; }
public Lot(int id)
{
this.id = id;
this.Name = "Lot n° " + id.ToString();
}
}
你能分享我们的地段模式吗? – mindOfAi
我刚刚做了,感谢您的关注! –