【发布时间】:2020-08-12 07:46:30
【问题描述】:
我有以下ListView-
<ListView x:Name="listViewm" ItemsSource="{ Binding Rows }">
<ListView.Header >
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"></ColumnDefinition>
<ColumnDefinition Width="1*"></ColumnDefinition>
<ColumnDefinition Width="1*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Text="Name" Grid.Column="3" FontSize="Medium" />
<Label Text="Age" Grid.Column="2" FontSize="Medium" />
<Label Text="DOB" Grid.Column="1" FontSize="Medium" />
</Grid>
</ListView.Header>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"></ColumnDefinition>
<ColumnDefinition Width="1*"></ColumnDefinition>
<ColumnDefinition Width="1*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<!--I need my Row values here-->
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我需要为列表中的每个条目创建一行并将其动态添加到网格中 -
protected override void OnAppearing()
{
CreateGrid();
}
public ObservableCollection<Row> Rows { get; set; }
public class Row
{
public string Name { get; set; }
public string Age { get; set; }
public string DOB { get; set; }
}
public void CreateGrid()
{
Rows = new ObservableCollection<Row>();
Rows.Clear();
foreach(var entry in entryList) { // entryList just contains values I use to populate row info
var row = new Row();
row.Name = entry.name;
row.Age = entry.age;
row.DOB = entry.dob;
Rows.Add(row);
}
}
itemSource 未绑定,因此没有创建新行,我尝试在 XAML 中删除 ItemsSource="{ Binding Rows }",并在后面的代码中使用 listViewm.ItemsSource = Rows,但这也不起作用。
因此,如果entryList 有 3 个针对 John、Bob 和 Bill 的测试条目,那么表格应该如下所示 -
+----------------+
| Name Age DOB |
+----------------+
| John 31 06/09 |
| Bill 32 07/10 |
| Bob 34 08/11 |
+----------------+
【问题讨论】:
标签: c# xaml listview xamarin.forms