具有行和列的 Xamarin 跨平台 Table
Xamarin Cross Platform Table with rows and columns
我想制作一个简单的 table 行和列:
C1 C2 C3 C4
R1 R1 R1 R1
R2 R2 R2 R2
等等。
起初我开始制作一个网格并将标签分配给列和行,但很难管理它并执行诸如过滤、重新排列和删除行之类的操作。我还能做什么?
编辑:
为了删除行,我使用了一个字典,其中每行和行号都有一个代表值。之后我就用了layout.Children.RemoveAt(rowNumber)
。问题是其他行没有重新定位并且它们之间存在间隙。我想我将不得不为每一行删除重新制作布局。
试试这个,这可能是最简单的方法,创建一个 ViewItem 和一个 ViewModel 项目,视图将是这样的:
public class NearMeView : ContentPage
{
private ListView _listView;
private MyViewModel _viewModel;
public NearMeView ()
{
_viewModel = new MyViewModel ();
BindingContext = _viewModel;
_listView = new ListView (){
ItemTemplate = CreateItemTemplate ()//Here you will set th data templates for each row and in each label nameLabel.SetBinding<Item> (Label.TextProperty, x => x.Name);
};
_listView.SetBinding<MyViewModel> (ListView.ItemsSourceProperty, x => x.Rows);
}
public class MyViewModel : ViewModel
{
List<Item>Rows;//will be populated from your DB each item will represent a Row, so you just need to remove from this list the item and automatically from the View should be removed the row.
// Method that you need to remove filter etc.
}
private DataTemplate CreateItemTemplate ()
{//this metod create the layout that you want to apply for each row in this example there are 3 columns, in each column there is a label, each label is binded to an element of the ItemSourceProperty of the ListView
return new DataTemplate (() => {
var nameLabel = new Label ();
var typeLabel = new Label ();
var distanceLabel = new Label (){ HorizontalOptions = LayoutOptions.EndAndExpand,VerticalOptions = LayoutOptions.Center };
nameLabel.SetBinding<BranchModel> (Label.TextProperty, x => x.Name);
typeLabel.SetBinding<BranchModel> (Label.TextProperty, x => x.Type);
distanceLabel.SetBinding<BranchModel> (Label.TextProperty,x=> x.Distance);
var leftStack = new StackLayout (){
Orientation = StackOrientation.Vertical,
VerticalOptions = LayoutOptions.Center,
Children = { nameLabel, typeLabel },
Padding = new Thickness (8,0,0,0)
};
return new ViewCell () {
View = new StackLayout () {
Orientation = StackOrientation.Horizontal,
Spacing = 5,
Children = { leftStack, distanceLabel },
Padding = new Thickness (0,0,0,8)
}
};
});
}
这几乎就是我用来使用 SearchBar 过滤列表的代码
我想制作一个简单的 table 行和列:
C1 C2 C3 C4
R1 R1 R1 R1
R2 R2 R2 R2
等等。
起初我开始制作一个网格并将标签分配给列和行,但很难管理它并执行诸如过滤、重新排列和删除行之类的操作。我还能做什么?
编辑:
为了删除行,我使用了一个字典,其中每行和行号都有一个代表值。之后我就用了layout.Children.RemoveAt(rowNumber)
。问题是其他行没有重新定位并且它们之间存在间隙。我想我将不得不为每一行删除重新制作布局。
试试这个,这可能是最简单的方法,创建一个 ViewItem 和一个 ViewModel 项目,视图将是这样的:
public class NearMeView : ContentPage
{
private ListView _listView;
private MyViewModel _viewModel;
public NearMeView ()
{
_viewModel = new MyViewModel ();
BindingContext = _viewModel;
_listView = new ListView (){
ItemTemplate = CreateItemTemplate ()//Here you will set th data templates for each row and in each label nameLabel.SetBinding<Item> (Label.TextProperty, x => x.Name);
};
_listView.SetBinding<MyViewModel> (ListView.ItemsSourceProperty, x => x.Rows);
}
public class MyViewModel : ViewModel
{
List<Item>Rows;//will be populated from your DB each item will represent a Row, so you just need to remove from this list the item and automatically from the View should be removed the row.
// Method that you need to remove filter etc.
}
private DataTemplate CreateItemTemplate ()
{//this metod create the layout that you want to apply for each row in this example there are 3 columns, in each column there is a label, each label is binded to an element of the ItemSourceProperty of the ListView
return new DataTemplate (() => {
var nameLabel = new Label ();
var typeLabel = new Label ();
var distanceLabel = new Label (){ HorizontalOptions = LayoutOptions.EndAndExpand,VerticalOptions = LayoutOptions.Center };
nameLabel.SetBinding<BranchModel> (Label.TextProperty, x => x.Name);
typeLabel.SetBinding<BranchModel> (Label.TextProperty, x => x.Type);
distanceLabel.SetBinding<BranchModel> (Label.TextProperty,x=> x.Distance);
var leftStack = new StackLayout (){
Orientation = StackOrientation.Vertical,
VerticalOptions = LayoutOptions.Center,
Children = { nameLabel, typeLabel },
Padding = new Thickness (8,0,0,0)
};
return new ViewCell () {
View = new StackLayout () {
Orientation = StackOrientation.Horizontal,
Spacing = 5,
Children = { leftStack, distanceLabel },
Padding = new Thickness (0,0,0,8)
}
};
});
}
这几乎就是我用来使用 SearchBar 过滤列表的代码