【发布时间】:2016-11-07 03:02:55
【问题描述】:
我试图让简单的绑定在 Xamarin Listview 上工作,但没有任何运气。 我没有收到任何错误。我刚得到一个 ListView,大概有 10 行左右的空行。
这是我正在使用的 XAML 代码。
<ContentView.Content >
<StackLayout>
<ListView x:Name="listView" ItemsSource="{Binding myItems}" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding TName}"/>
<Label Text ="{Binding Num, StringFormat='${0}'}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
后面的代码
public partial class TestView : ContentView
{
public TestView()
{
InitializeComponent();
listView.BindingContext = this;
}
public static ObservableCollection<Test> myItems
{
get
{
return new ObservableCollection<Test>(new[]
{
new Test() { TName = "Item 1", Num = 1 },
new Test() { TName = "Item 2", Num = 2 },
new Test() { TName = "Item 3", Num = 3 },
new Test() { TName = "Item 4", Num = 4 },
});
}
}
}
public class Test
{
public string TName { get; set; }
public int Num { get; set; }
}
我开始相信 Xamarin 不是我的朋友 :(.
【问题讨论】:
-
this.BindingContext = this;
-
谢谢杰森!我必须添加它并更改从 myItems 中删除静态。如果你把它作为答案,我会标记它。