【问题标题】:UWP GridView TableUWP GridView 表
【发布时间】:2017-04-14 17:52:58
【问题描述】:

我想用 HTML 制作一个表格。所以我从我的数据库中获取了一些数据。

每个项目都是一个User。用户有用户名、名字、姓氏和电子邮件。我想制作一个表格来列出这些用户。

每个用户都必须换行。我已经在互联网上搜索过,但没有找到我想要的。

如果有人可以帮助我,那就太好了。

【问题讨论】:

标签: gridview html-table win-universal-app


【解决方案1】:

使用以下框架代码并围绕它进行构建(例如添加相关的 GridView 属性,例如边框粗细/颜色、宽度等,当然还可以从您的数据库中填充数据绑定)。同样,数据模板中 Grid 的属性。

下面的 xaml 背后的代码没有显示,因为它不包含用户代码,而是自动添加的页面初始化。

此代码已经过测试并且可以工作。

<Page x:Class="Sample.GridViewTestPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:data="using:Sample.Data"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d">

    <Page.Resources>
        <data:UserDataCollection x:Key="userDataCollection"/>
    </Page.Resources>

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

        <GridView ItemsSource="{StaticResource userDataCollection}"
                  IsItemClickEnabled="True"
                  IsSwipeEnabled="true"
                  SelectionMode="Single">

            <GridView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapGrid Orientation="Vertical"/>
                </ItemsPanelTemplate>
            </GridView.ItemsPanel>

            <GridView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="200"/>
                            <ColumnDefinition Width="200"/>
                            <ColumnDefinition Width="200"/>
                            <ColumnDefinition Width="200"/>
                        </Grid.ColumnDefinitions>

                        <TextBlock Grid.Column="0" Text="{Binding UserName}"/>
                        <TextBlock Grid.Column="1" Text="{Binding FirstName}"/>
                        <TextBlock Grid.Column="2" Text="{Binding LastName}"/>
                        <TextBlock Grid.Column="3" Text="{Binding Email}"/>
                    </Grid>
                </DataTemplate>
            </GridView.ItemTemplate>
        </GridView>

    </Grid>
</Page>

下面是UserData类模型和UserData对象集合进行绑定。这很容易弄清楚。

using System.Collections.ObjectModel;

namespace Sample.Data
{
    class UserDataCollection: ObservableCollection<UserData>
    {
        public UserDataCollection()
        {
            // Sample data loaded here. Replace with data from your database

            this.Add(new UserData() {
                UserName = "user1",
                FirstName = "FN1",
                LastName = "LN1",
                Email = "user1@nowhere.local" });

            this.Add(new UserData() {
                UserName = "user2",
                FirstName = "FN2",
                LastName = "LN2",
                Email = "user2@nowhere.local" });

            this.Add(new UserData() {
                UserName = "user3",
                FirstName = "FN3",
                LastName = "LN3",
                Email = "user3@nowhere.local" });
        }
    }

    public class UserData
    {
        public string UserName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
    }
}

【讨论】:

  • 哦,太感谢了,如果我想在这个表中添加一个标题,我该怎么做?我只想为每一列添加一个标题。
  • 只需将第一行数据设为您的列名。在上面用列名替换与“user1”对应的第一个数据样本。有意义吗?
  • 是的,我明白你在说什么,但没有更好的方法吗?
  • 我在尝试使用 this.Add() 时遇到错误。错误消息是Non-invocable member 'ClassName.Add' cannot be used like a method.。请帮忙。
  • 您确实需要设置特定的宽度,Width="Auto" 并没有整齐地排列在列中。
【解决方案2】:

您好,您可以使用 StackPanel 添加列名,如下所示 为每个 TextBlock 提供与 Column Width 相同的宽度并选择 TextAlignment 作为 中心

      <StackPanel Orientation="Horizontal">
            <TextBlock
                Width="200"
                Text="UserName"
                TextAlignment="Center"
                />

            <TextBlock
                Width="200"
                Text="IMEI"
                TextAlignment="Center"
                />
            <TextBlock
                Width="200"
                Text="FirstName"
                TextAlignment="Center"
                />
            <TextBlock
                Width="200"
                Text="LastName"
                TextAlignment="Center"
                />
            <TextBlock
                Width="200"
                Text="Email"
                TextAlignment="Center"
                />

        </StackPanel>

【讨论】:

    猜你喜欢
    • 2016-01-26
    • 1970-01-01
    • 2020-05-31
    • 2018-03-21
    • 2017-07-22
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 2019-09-24
    相关资源
    最近更新 更多