简体   繁体   English

我如何使用x:Bind和System.Collections.Generic.List填充ListView并生成列

[英]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: 除了绑定,我得到的结果是:

该应用程序的快照,说明ItemsSource可以正常工作,但不按预期显示,仅显示:每个ListViewItem中的AuraFaktura.Customer

Update 更新资料

I have made some progress today and i now have correct data output. 我今天取得了一些进展,现在我的数据输出正确。 I found out that with the use DataTemplate's i can set Binding on elements. 我发现使用DataTemplate可以在元素上设置Binding。

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: 结果:

该应用程序的快照说明DataTemplate在某种程度上可以正常工作,但由于项目被截断并且标题项未显示,因此无法按预期显示。

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.

相关问题 无法隐式转换类型'System.Collections.Generic.List<x> ' 到 'System.Collections.Generic.List<y> '</y></x> - Cannot implicitly convert type 'System.Collections.Generic.List<x>' to 'System.Collections.Generic.List<y>' 如何调试“无法转换为 system.collections.generic.list”错误 - How do I debug “cannot convert to system.collections.generic.list” error 如何修复“无法将类型System.Collections.Generic.List &lt;&gt;隐式转换为System.Collections.Generic.List &lt;&gt;” - How to fix 'Cannot implicitly convert type System.Collections.Generic.List<> to System.Collections.Generic.List<>' 在Python中使用System.Collections.Generic.List`1 [System.Byte] - Using System.Collections.Generic.List`1[System.Byte] in Python 我如何访问 System.Collections.Generic.List`1 [mySolution.Models.PermissionsModel] 类型的会话并进行一些测试 - how can i acces to my session of type System.Collections.Generic.List`1 [mySolution.Models.PermissionsModel] and do some tests 我得到了“ System.Collections.Generic.List”而不是数据 - I got “System.Collections.Generic.List” instead of data 如何解决“'System.Collections.Generic.List<string> ' 到 'System.Collections.Generic.List<system.web.mvc.selectlistitem> ''</system.web.mvc.selectlistitem></string> - how to resolve "'System.Collections.Generic.List<string>' to 'System.Collections.Generic.List<System.Web.Mvc.SelectListItem>'' 如何转换system.collections.generic.list <className> 串 - How to convert system.collections.generic.list<className> to string 如何处理类型“ Object {System.Collections.Generic.List <object> }” - How to handle type “Object {System.Collections.Generic.List<object>}” system.collections.generic.list 到 system.collections.generic.ienumerable - system.collections.generic.list to system.collections.generic.ienumerable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM