使用 List<list<string>> 作为源和网格作为数据模板在代码隐藏中创建列表视图
Creat a listview in cide behind with List<list<string>> as a source and grid as data template
Structure.Values 是列表<列表>
在后面的代码中我设置了 listviewdata 模板
var grid = new grid();
grid.columnspacing = 0;
grid.rowspacing = 0;
grid.backgroundcolor = color.black;
grid.columndefinitions = new columndefinitioncollection();
grid.rowdefinitions = new rowdefinitioncollection();
grid = creategridrowscolumns(datalist, grid);
var tapgesturerecognizer = new tapgesturerecognizer();
tapgesturerecognizer.setbinding(tapgesturerecognizer.commandproperty, new binding("bindingcontext.detailscommand", source: listviewm));
tapgesturerecognizer.setbinding(tapgesturerecognizer.commandparameterproperty, ".");
grid.gesturerecognizers.add(tapgesturerecognizer);
并将网格数据设置为
private Grid CreateGridRowsColumns(IEnumerable list, Grid grid)
{
var lst = list as List<List<string>>;
int row = 0;
foreach (var ro in lst)
{
int column = 0;
foreach (var col in ro)
{
Label title = new Label
{
Text = col,
Style = GetResourceValue("DataLabelStyle") as Style
};
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
grid.Children.Add(title, column,row);
column++;
}
row++;
}
return grid;
}
那么如何使用listview产生同样的效果
编辑:
我的视图模型
public class BaseListViewModel : BaseViewModel
{
public ExcelStructure Structure { get; private set; }
}
和型号:
public class ExcelStructure
{
public List<string> Headers { get; set; } = new List<string>();
public List<List<string>> Values { get; set; } = new List<List<string>>();
}
我不确定你到底想要什么。你想在每个项目中有许多列吗? ListView中的每一项都相当于Grid中的一行,然后一行中有好几列对不对?
如果是,您可以尝试以下操作:
public ListGridPage()
{
InitializeComponent();
BaseListViewModel listModel = new BaseListViewModel();
BindingContext = listModel;
listViewm.ItemTemplate = new DataTemplate(() =>
{
return new MyViewCell();
});
}
class MyViewCell : ViewCell
{
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
List<string> structure = (List<string>)BindingContext;
var grid = new Grid();
grid.ColumnSpacing = 0;
grid.RowSpacing = 0;
grid.ColumnDefinitions = new ColumnDefinitionCollection();
grid.RowDefinitions = new RowDefinitionCollection();
grid = Creategridrowscolumns(structure, grid);
this.View = grid;
}
private Grid Creategridrowscolumns(List<string> structure, Grid grid)
{
var lst = structure as List<string>;
int column = 0;
foreach (var col in structure)
{
Label title = new Label
{
Text = col,
};
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(200, GridUnitType.Star) });
grid.Children.Add(title, column, 0);
column++;
}
return grid;
}
}
视图模型:
class BaseListViewModel : BaseViewModel
{
public ExcelStructure Structure { get; private set; }
public BaseListViewModel()
{
Structure = new ExcelStructure();
}
}
型号:
public class ExcelStructure
{
public List<List<string>> Values { get; set; }
public ExcelStructure()
{
Values = new List<List<string>>();
Values.Add(new List<string>() {"1-A", "1-B", "1-C", "1-D", "1-E" });
Values.Add(new List<string>() { "2-A", "2-B", "2-C", "2-D", "2-E" });
Values.Add(new List<string>() { "3-A", "3-B", "3-C", "3-D", "3-E" });
Values.Add(new List<string>() { "4-A", "4-B", "4-C", "4-D", "4-E" });
}
}
效果如:
Structure.Values 是列表<列表>
在后面的代码中我设置了 listviewdata 模板
var grid = new grid();
grid.columnspacing = 0;
grid.rowspacing = 0;
grid.backgroundcolor = color.black;
grid.columndefinitions = new columndefinitioncollection();
grid.rowdefinitions = new rowdefinitioncollection();
grid = creategridrowscolumns(datalist, grid);
var tapgesturerecognizer = new tapgesturerecognizer();
tapgesturerecognizer.setbinding(tapgesturerecognizer.commandproperty, new binding("bindingcontext.detailscommand", source: listviewm));
tapgesturerecognizer.setbinding(tapgesturerecognizer.commandparameterproperty, ".");
grid.gesturerecognizers.add(tapgesturerecognizer);
并将网格数据设置为
private Grid CreateGridRowsColumns(IEnumerable list, Grid grid)
{
var lst = list as List<List<string>>;
int row = 0;
foreach (var ro in lst)
{
int column = 0;
foreach (var col in ro)
{
Label title = new Label
{
Text = col,
Style = GetResourceValue("DataLabelStyle") as Style
};
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
grid.Children.Add(title, column,row);
column++;
}
row++;
}
return grid;
}
那么如何使用listview产生同样的效果
编辑:
我的视图模型
public class BaseListViewModel : BaseViewModel
{
public ExcelStructure Structure { get; private set; }
}
和型号:
public class ExcelStructure
{
public List<string> Headers { get; set; } = new List<string>();
public List<List<string>> Values { get; set; } = new List<List<string>>();
}
我不确定你到底想要什么。你想在每个项目中有许多列吗? ListView中的每一项都相当于Grid中的一行,然后一行中有好几列对不对?
如果是,您可以尝试以下操作:
public ListGridPage()
{
InitializeComponent();
BaseListViewModel listModel = new BaseListViewModel();
BindingContext = listModel;
listViewm.ItemTemplate = new DataTemplate(() =>
{
return new MyViewCell();
});
}
class MyViewCell : ViewCell
{
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
List<string> structure = (List<string>)BindingContext;
var grid = new Grid();
grid.ColumnSpacing = 0;
grid.RowSpacing = 0;
grid.ColumnDefinitions = new ColumnDefinitionCollection();
grid.RowDefinitions = new RowDefinitionCollection();
grid = Creategridrowscolumns(structure, grid);
this.View = grid;
}
private Grid Creategridrowscolumns(List<string> structure, Grid grid)
{
var lst = structure as List<string>;
int column = 0;
foreach (var col in structure)
{
Label title = new Label
{
Text = col,
};
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(200, GridUnitType.Star) });
grid.Children.Add(title, column, 0);
column++;
}
return grid;
}
}
视图模型:
class BaseListViewModel : BaseViewModel
{
public ExcelStructure Structure { get; private set; }
public BaseListViewModel()
{
Structure = new ExcelStructure();
}
}
型号:
public class ExcelStructure
{
public List<List<string>> Values { get; set; }
public ExcelStructure()
{
Values = new List<List<string>>();
Values.Add(new List<string>() {"1-A", "1-B", "1-C", "1-D", "1-E" });
Values.Add(new List<string>() { "2-A", "2-B", "2-C", "2-D", "2-E" });
Values.Add(new List<string>() { "3-A", "3-B", "3-C", "3-D", "3-E" });
Values.Add(new List<string>() { "4-A", "4-B", "4-C", "4-D", "4-E" });
}
}
效果如: