您不需要创建这种条目。
你想达到这样的效果吗?
如果是这样,您应该在您的 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