0
我试图得到简单的绑定工作Xamarin Listview没有任何运气。 我没有得到任何错误。我只是得到一个ListView可能有10个左右的空行。Xamarin列表视图绑定不起作用
这是我使用的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的代码是不是我的朋友:(。
thi s.BindingContext = this; – Jason
谢谢杰森!我不得不添加,并更改从myItems中删除静态。 如果你把它作为答案,我会标记它的权利。 – jbassking10