[英]How do i populate a ListView using x:Bind and System.Collections.Generic.List and generate columns
I am trying to populate a ListView with a List while setting the ItemsSource as a x:Bind
, and i cant figure out how to make columns and make the items display properly. 我试图在将ItemsSource设置为
x:Bind
同时用列表填充ListView ,但我不知道如何制作列并使项目正确显示。
Here is what i got so far: 这是我到目前为止所得到的:
The Customer class in Database.cs Database.cs中的Customer类
public class Customer
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string CompanyName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Phone { get; set; }
public string Email { get; set; }
public string Adress { get; set; }
public int PostCode { get; set; }
public string PostArea { get; set; }
public string CreatedDate { get; set; }
public string UpdatedDate { get; set; }
}
The FetchCustomer method in Database.cs that returns a list of Customer objects Database.cs中的FetchCustomer方法返回Customer对象的列表
public List<Customer> FetchCustomer()
{
using (SQLiteConnection _connection = new SQLiteConnection(sqlitePlatform, dbFilePath))
{
List<Customer> customers = new List<Customer>();
var _customers = _connection.Table<Customer>();
foreach (Customer customer in _customers)
{
customers.Add(customer);
}
return customers;
}
}
The CustomerView Page CustomerView页面
public sealed partial class CustomerView : Page
{
public List<Customer> CustomersFromDB { get; set; }
public CustomerView()
{
this.InitializeComponent();
ReadyCustomers();
}
private void ReadyCustomers()
{
var db = new Database();
CustomersFromDB = db.FetchCustomer();
}
}
The XAML where i bind the ItemsSource 我绑定ItemsSource的XAML
<ListView Grid.Row="1" ItemsSource="{x:Bind CustomersFromDB}"/>
How do i go forward making this so that columns are auto generated and the items display properly? 我该如何进行此操作,以便自动生成列并正确显示项目?
I have tried to add ListViewHeaderItem s and browse through the differerent properties but havent found out how yet. 我试图添加ListViewHeaderItem并浏览不同的属性,但还没有找到方法。
<ListView Grid.Row="1" ItemsSource="{x:Bind CustomersFromDB}">
<ListView.Header>
<ListViewHeaderItem ???></ListViewHeaderItem>
</ListView.Header>
</ListView>
The result i get with nothing else than the binding is this: 除了绑定,我得到的结果是:
I still struggle with showing the data in correct manner. 我仍然在努力以正确的方式显示数据。
Here is the XAML i now have written: 这是我现在编写的XAML:
<GridView Grid.Row="1" ItemsSource="{x:Bind CustomersFromDB}" HorizontalAlignment="Stretch"> <GridViewHeaderItem Content="CompanyName"></GridViewHeaderItem> <GridViewHeaderItem Content="FirstName"></GridViewHeaderItem> <GridViewHeaderItem Content="LastName"></GridViewHeaderItem> <GridViewHeaderItem Content="Phone"></GridViewHeaderItem> <GridViewHeaderItem Content="Email"></GridViewHeaderItem> <GridViewHeaderItem Content="Adress"></GridViewHeaderItem> <GridViewHeaderItem Content="PostCode"></GridViewHeaderItem> <GridViewHeaderItem Content="PostArea"></GridViewHeaderItem> <GridViewHeaderItem Content="Comment"></GridViewHeaderItem> <GridView.ItemTemplate> <DataTemplate> <GridViewItem BorderThickness="0" BorderBrush="Transparent"> <GridViewItem.Content> <StackPanel Orientation="Horizontal" BorderThickness="0" BorderBrush="Transparent"> <TextBlock Text="{Binding CompanyName}"></TextBlock> <TextBlock Text="{Binding FirstName}"></TextBlock> <TextBlock Text="{Binding LastName}"></TextBlock> <TextBlock Text="{Binding Phone}"></TextBlock> <TextBlock Text="{Binding Email}"></TextBlock> <TextBlock Text="{Binding Adress}"></TextBlock> <TextBlock Text="{Binding PostCode}"></TextBlock> <TextBlock Text="{Binding PostArea}"></TextBlock> <TextBlock Text="{Binding Comment}"></TextBlock> <TextBlock Text="{Binding CreatedDate}"></TextBlock> <TextBlock Text="{Binding UpdatedDate}"></TextBlock> </StackPanel> </GridViewItem.Content> </GridViewItem> </DataTemplate> </GridView.ItemTemplate> </GridView>
And the result: 结果:
So as you can se the GridViewHeaderItem's are not showing and the GridViewItem's are not showing properly. 因此,您可以看到GridViewHeaderItem的未显示且GridViewItem的未正确显示。
Here is the result i want to end up with: 这是我要结束的结果:
How would i go forward writing my XAML to achieve such a result? 我将如何继续编写XAML以实现这样的结果?
只需将边距应用于文本块margin =“ 5,0,5,0”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.