如何在自定义 Xamarin.Forms ViewCell 的行之间添加分隔符 space?
How to add a separator space between rows on custom Xamarin.Forms ViewCell?
在this question on Xamarin Forums中,
Craig Dunn 教授如何创建带有框架的单元格。
我想在每个单元格之间添加一个 space。
目前细胞似乎粘在一起,ViewCell
没有 space 属性。
如何在自定义 Xamarin.Forms ViewCell 的行之间添加分隔符 space?
您只需进一步自定义 MenuCell
的布局即可。
下面显示的版本使用进一步的 Xamarin.Forms.Frame
在每个项目之间创建间距,并进行了一些其他修改:-
XAML 页:-
<ListView x:Name="lstItems" />
XAML 代码隐藏:-
lstItems.ItemTemplate = new DataTemplate(typeof(Classes.MenuCell));
lstItems.ItemsSource = new string[] { "Apples", "Bananas", "Pears", "Oranges" };
ViewCell class:-
public class MenuCell : ViewCell
{
public MenuCell()
{
Label objLabel = new Label
{
YAlign = TextAlignment.Center,
TextColor = Color.Yellow,
};
objLabel.SetBinding(Label.TextProperty, new Binding("."));
StackLayout objLayout = new StackLayout
{
Padding = new Thickness(20, 0, 0, 0),
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.StartAndExpand,
Children = { objLabel }
};
Frame objFrame_Inner = new Frame
{
Padding = new Thickness(15, 15, 15, 15),
HeightRequest = 36,
OutlineColor = Color.Accent,
BackgroundColor = Color.Blue,
Content = objLayout,
};
Frame objFrame_Outer = new Frame
{
Padding = new Thickness(0, 0, 0, 10),
Content = objFrame_Inner
};
View = objFrame_Outer;
}
}
将产生以下结果:-
我的xaml例子:
<ListView BackgroundColor="Gray" SeparatorVisibility="None" ItemsSource="{Binding Shipments}" x:Name="lstShipments" RowHeight="60">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="0,0,0,1">
<Grid VerticalOptions="Fill" BackgroundColor="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"></ColumnDefinition>
<ColumnDefinition Width="60"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Grid.Row="0" Text="{Binding DestinationCountry}" FontSize="16" />
<Image Grid.Column="1" Grid.Row="0" Source="box32.png" />
<Label Grid.Column="0" Grid.Row="1" Text="{Binding ExchangeOfficeDestinationTitle}" FontSize="16" />
<Label Grid.Column="1" Grid.Row="1" Text="{Binding ShipmentNum}" FontSize="10" />
</Grid>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在this question on Xamarin Forums中, Craig Dunn 教授如何创建带有框架的单元格。
我想在每个单元格之间添加一个 space。
目前细胞似乎粘在一起,ViewCell
没有 space 属性。
如何在自定义 Xamarin.Forms ViewCell 的行之间添加分隔符 space?
您只需进一步自定义 MenuCell
的布局即可。
下面显示的版本使用进一步的 Xamarin.Forms.Frame
在每个项目之间创建间距,并进行了一些其他修改:-
XAML 页:-
<ListView x:Name="lstItems" />
XAML 代码隐藏:-
lstItems.ItemTemplate = new DataTemplate(typeof(Classes.MenuCell));
lstItems.ItemsSource = new string[] { "Apples", "Bananas", "Pears", "Oranges" };
ViewCell class:-
public class MenuCell : ViewCell
{
public MenuCell()
{
Label objLabel = new Label
{
YAlign = TextAlignment.Center,
TextColor = Color.Yellow,
};
objLabel.SetBinding(Label.TextProperty, new Binding("."));
StackLayout objLayout = new StackLayout
{
Padding = new Thickness(20, 0, 0, 0),
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.StartAndExpand,
Children = { objLabel }
};
Frame objFrame_Inner = new Frame
{
Padding = new Thickness(15, 15, 15, 15),
HeightRequest = 36,
OutlineColor = Color.Accent,
BackgroundColor = Color.Blue,
Content = objLayout,
};
Frame objFrame_Outer = new Frame
{
Padding = new Thickness(0, 0, 0, 10),
Content = objFrame_Inner
};
View = objFrame_Outer;
}
}
将产生以下结果:-
我的xaml例子:
<ListView BackgroundColor="Gray" SeparatorVisibility="None" ItemsSource="{Binding Shipments}" x:Name="lstShipments" RowHeight="60">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="0,0,0,1">
<Grid VerticalOptions="Fill" BackgroundColor="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"></ColumnDefinition>
<ColumnDefinition Width="60"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Grid.Row="0" Text="{Binding DestinationCountry}" FontSize="16" />
<Image Grid.Column="1" Grid.Row="0" Source="box32.png" />
<Label Grid.Column="0" Grid.Row="1" Text="{Binding ExchangeOfficeDestinationTitle}" FontSize="16" />
<Label Grid.Column="1" Grid.Row="1" Text="{Binding ShipmentNum}" FontSize="10" />
</Grid>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>