【发布时间】:2019-08-18 06:12:22
【问题描述】:
我看到一篇关于 xamarin 表单的文章,其中包含一些“可绑定的爱”https://blog.xamarin.com/xamarin-forms-3-5-a-little-bindable-love/。
我想尝试一下并创建一个滚动列表,但由于某种原因,我绑定的数据似乎没有出现在视图中。绑定应该可以正常工作,因为在我的 XAML 中我绑定了很多其他东西并且它可以成功运行,但是由于某种原因,这个特定的绑定不起作用。
XAML:
<ScrollView>
<StackLayout BackgroundColor="Transparent" BindableLayout.ItemsSource="{Binding CategoriesList}" >
<BindableLayout.ItemTemplate>
<DataTemplate>
<StackLayout Orientation = "Horizontal" HeightRequest="150" WidthRequest="80">
<Button Text="{Binding Name}" />
</StackLayout>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</ScrollView>
这是我的列表在 VM 中的外观:
private ObservableCollection<Categories> _Categories;
public ObservableCollection<Categories> CategoriesList
{
get
{
return this._Categories;
}
set
{
if (_Categories != value)
{
this._Categories = value;
RaisePropertyChanged("CategoriesList");
}
}
}
当我为它创建数据时:
CategoriesList = new ObservableCollection<Categories>();
CategoriesList.Add(new Categories() { Name = "Data 1", ID = "1" });
CategoriesList.Add(new Categories() { Name = "Data 2", ID = "2" });
CategoriesList.Add(new Categories() { Name = "Data 3", ID = "3" });
【问题讨论】:
标签: c# xamarin.forms binding