Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a separator space between rows on custom Xamarin.Forms ViewCell?

In this question on Xamarin Forums, Craig Dunn teaches how to create a cell with frame.

I want to Add a space between each cell.

At present the cells seems glued, and the ViewCell doesn`t have a space property.

How can I add a separator space between rows on custom Xamarin.Forms ViewCell?

like image 936
Ineltec Android Avatar asked Jun 12 '15 15:06

Ineltec Android


People also ask

How do I give a space in Xamarin?

To get dynamic spacing similar to CSS "Space-Between" in Xamarin you can insert filler views between your views that actually have content. Omit the filler view after the last "actual" view so that the view you want is at the end (the "-50" label at the bottom in the example).

How do I add a line in Xamarin?

To draw a line, create a Line object and set its X1 and Y1 properties to its start point, and its X2 and Y properties to its end point. In addition, set its Stroke property to a Brush -derived object because a line without a stroke is invisible. For more information about Brush objects, see Xamarin.


2 Answers

You just have to customize the layout of the MenuCell further to achieve this.

Shown below is a version that uses a further Xamarin.Forms.Frame to create a spacing between each item with a couple other modifications:-

XAML Page:-

<ListView x:Name="lstItems" />

XAML Code-Behind:-

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;            
    }
}

Will result in the following:-

ListView with spacing between each item.

like image 119
Pete Avatar answered Oct 28 '22 03:10

Pete


my xaml example:

 <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>

enter image description here

like image 32
ahaliav fox Avatar answered Oct 28 '22 03:10

ahaliav fox