0

有谁知道是否可以实现如下图所示的条目?我想要一个数据网格来显示数据库中的数据。我曾尝试使用 Xamarin.Forms.Datagrid,但它不允许手动添加行。我的目标是以更容易查看的方式显示参数(类似于 excel 工作表格式),但 Xamarin 的控件确实有限......只是想知道是否有人知道其他方法来实现它?

在此处输入图像描述

这是我的数据网格代码。我只能用名称而不是行来创建列。

<dg:DataGrid HeaderHeight="50"
         BorderColor="#CCCCCC" HeaderBackground="#E0E6F8">
    <dg:DataGrid.Columns>
        <dg:DataGridColumn Title="Zone 1"/>
        <dg:DataGridColumn Title="Zone 2"/>
        <dg:DataGridColumn Title="Zone 3"/>
        <dg:DataGridColumn Title="Zone 4"/>
        <dg:DataGridColumn Title="Zone 5"/>
        <dg:DataGridColumn Title="Zone 6"/>
    </dg:DataGrid.Columns>
</dg:DataGrid>

在此处输入图像描述

4

1 回答 1

1

您不需要创建这种条目。

你想达到这样的效果吗? 在此处输入图像描述

如果是这样,您应该在您的 xaml 文件夹中添加此布局。

 <StackLayout>
        <!-- Place new controls here -->
        <dg:DataGrid ItemsSource="{Binding Teams}" SelectionEnabled="True"
               RowHeight="70" HeaderHeight="50" BorderColor="#CCCCCC" HeaderBackground="#E0E6F8">

            <dg:DataGrid.HeaderFontSize>
                <OnIdiom  x:TypeArguments="x:Double">
                    <OnIdiom.Tablet>15</OnIdiom.Tablet>
                    <OnIdiom.Phone>13</OnIdiom.Phone>
                </OnIdiom>
            </dg:DataGrid.HeaderFontSize>

            <dg:DataGrid.Columns>


                <dg:DataGridColumn Title="Team" PropertyName="Name" Width="2*"/>
                <dg:DataGridColumn Title="Win" PropertyName="Win" Width="0.95*"/>
                <dg:DataGridColumn Title="Loose" PropertyName="Loose"  Width="1*"/>
                <dg:DataGridColumn Title="Home" PropertyName="Home"/>
                <dg:DataGridColumn Title="Percentage" PropertyName="Percentage" StringFormat="{}{0:0.00}" />

            </dg:DataGrid.Columns>


        </dg:DataGrid>
    </StackLayout>

如果您不知道如何在此控件中添加行。例如,

<dg:DataGridColumn Title="Team" PropertyName="Name" Width="2*"/>

上面的行有三个属性,Title, PropertyName, Width

Title:表示一线队、胜利、松散、主场、百分比冠军。

Width: 这个的平均宽度Column

如果你想添加数据自爆Title,你应该使用PropertyName属性。

在布局背景中,您添加绑定上下文。

 public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            this.BindingContext = new MyViewModel();
        }
    }

这是我的MyViewModel.cs

    public class MyViewModel
    {
        public ObservableCollection<Team> Teams { get; set; }
        public MyViewModel()
        {
            Teams = new ObservableCollection<Team>();

            Teams.Add(new Team() {  Home = "lost1", Win = "31", Loose="1", Name="cava", Percentage="91" }) ;
            Teams.Add(new Team() {  Home = "lost2", Win = "31", Loose = "2", Name = "cava", Percentage = "91" });
            Teams.Add(new Team() { Home = "lost3", Win = "31", Loose = "3", Name = "cava", Percentage = "91" });
            Teams.Add(new Team() {  Home = "lost4", Win = "31", Loose = "4", Name = "cava", Percentage = "91 "});
            Teams.Add(new Team() {  Home = "lost5", Win = "31", Loose = "5", Name = "cava", Percentage = "91" });
            Teams.Add(new Team() {  Home = "lost6", Win = "31", Loose = "6", Name = "cava", Percentage = "91",  });
        }
    }

这是我的Team代码。

  public class Team
    {

        public string Name { get; set; }
        public string Win { get; set; }
        public string Loose { get; set; }
        public string Home { get; set; }
        public string Percentage { get; set; }



    }

在此处输入图像描述

这是我的演示。

https://github.com/851265601/Xamarin.Android_ListviewSelect/blob/master/DataGridDemo1.zip

于 2020-04-28T06:32:37.717 回答