Change programmatically the Rows and Columns in Xamarin Forms - c#

I'm filling a grid with an observablecollection from my viewmodel but I want to get that grid formatted as follows:
(Note my model has no row/column information just calls the posts as shown in the code)
My code now looks like this:
<StackLayout BindableLayout.ItemsSource="{Binding Posts}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Grid Margin="5" RowSpacing="0" x:Name="postsTable">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="200"/>
</Grid.RowDefinitions>
<ImageButton Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="1" Source="{Binding postImage}" VerticalOptions="Fill" HorizontalOptions="Fill" Aspect="AspectFill" Clicked="News1_Clicked"/>
<StackLayout Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="1" VerticalOptions="End" HorizontalOptions="Start" Margin="10">
<Label Text="{Binding title}" FontFamily="{StaticResource BoldFont}" StyleClass="titlesqr"/>
<Label Text="{Binding subtitle}" FontFamily="{StaticResource NormalFont}" StyleClass="textsqr"/>
</StackLayout>
</Grid>
</DataTemplate>
</BindableLayout.ItemTemplate>
How can I put the rows / columns /columnspan information to achieve this view?

Related

Xamarin ListView alternate column color

I need to alternate my ListView columns color so it looks like this.
I was searching Google for any help, and found out that i can put BoxView in the same column with my Label and color that BoxView
My result:
As you can see, i have this annoying white line above BoxViews, which i was not able to remove. My question is how do i remove them? i havent set any paddings and margins, have no clue what is problem.
<ListView x:Name="ListView" ItemTapped="ListView_OnItemTapped" ItemsSource="{Binding FilteredReports, Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<BoxView HeightRequest="1" Color="#EDEBE9" IsVisible="true"/>
<Grid HorizontalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<BoxView Color="#F7FAFC" Grid.Column="0"/>
<Label Grid.Column="0" FontSize="12" FontFamily="Roboto" TextColor="#162938" Text="{Binding ClientPhone}" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
<Label Grid.Column="1" FontSize="12" FontFamily="Roboto" TextColor="#162938" Text="{Binding OfficeName}" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
<BoxView Color="#F7FAFC" Grid.Column="2"/>
<Label Grid.Column="2" FontSize="12" FontFamily="Roboto" TextColor="#162938" Text="{Binding BranchName}" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
</Grid>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
You are using a StackLayout and by default it has a Spacing property with a value of 6.
You should set it to 0
<ViewCell>
<StackLayout Spacing = "0" >
<BoxView HeightRequest="1" Color="#EDEBE9" IsVisible="true"/>
<Grid HorizontalOptions="FillAndExpand">
....
</StackLayout>
</ViewCell>

ListView with Grid containing 2 objects for each column

In my application, I would like to display a ListView, with a vertical scroll. Inside the ListView, I would like to insert a Grid divided into two columns. Each column would like to have a list of objects in Binding, so 2 objects for each row. I thought of creating 2 ListView, with simultaneous scrolling, so that when I scroll on the left one, it also happens on the right one, as if it were a single. But the work is longer. Is there anything simpler than being able to use only a ListView with two objects for each row, one for each column?
This is my code XAML:
<ListView x:Name="CategorySx"
RelativeLayout.XConstraint="{ConstraintExpression
Type=Constant,
Constant=55}"
WidthRequest="130" HasUnevenRows="True" ItemsSource="{Binding listSx}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid RowSpacing="10">
<Grid.RowDefinitions>
<RowDefinition Height="70"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image x:Name="SX" Grid.Row="0" Grid.Column="0" Source="{Binding ImageSx}"/>
<Label Grid.Row="0" Grid.Column="0" Text="{Binding TitoloSx}"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ListView x:Name="CategoryDx"
RelativeLayout.XConstraint="{ConstraintExpression
Type=Constant,
Constant=200}"
WidthRequest="130" HasUnevenRows="True" ItemsSource="{Binding listDx}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid RowSpacing="10">
<Grid.RowDefinitions>
<RowDefinition Height="70"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image x:Name="SX" Grid.Row="0" Grid.Column="0" Source="{Binding ImageDx}"/>
<Label Grid.Row="0" Grid.Column="0" Text="{Binding TitoloDx}"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</RelativeLayout>
Use a collectionView with Vertical grid would achieve the result easily:
<CollectionView ItemsSource="{Binding Monkeys}"
ItemsLayout="VerticalGrid, 2">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="35" />
<RowDefinition Height="35" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70" />
<ColumnDefinition Width="80" />
</Grid.ColumnDefinitions>
<Image Grid.RowSpan="2"
Source="{Binding ImageUrl}"
Aspect="AspectFill"
HeightRequest="60"
WidthRequest="60" />
<Label Grid.Column="1"
Text="{Binding Name}"
FontAttributes="Bold"
LineBreakMode="TailTruncation" />
<Label Grid.Row="1"
Grid.Column="1"
Text="{Binding Location}"
LineBreakMode="TailTruncation"
FontAttributes="Italic"
VerticalOptions="End" />
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>

Set CollectionView to Fill Entire Screen in XAML and Xamarin.Forms

I am building an app for a course that uses CollectionView's Grid, and on one screen, the grid is not filling the entire screen, as shown in the screenshot below. The XAML is nearly identical in all screens, with the exception of this one. Looking through the documentation at https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/layout, I'm not seeing anything that will force the table to fill. I have tried adding HorizontalOptions="FillAndExpand" and HorizontalOptions="CenterAndExpand" in the CollectionView declaration, but am seeing the same thing when I run the debugger. How can I make this stretch across the screen using CollectionView instead of ListView?
XAML Code:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Project.TermPage">
<ContentPage.ToolbarItems>
<ToolbarItem Text="Save"
Clicked="SaveButton_Clicked"/>
</ContentPage.ToolbarItems>
<NavigationPage.TitleView>
<Label Text="Edit Term Information"
HorizontalTextAlignment="Center"
FontFamily="{StaticResource FuturaStdMedium}"
TextColor="White"
FontSize="Title"/>
</NavigationPage.TitleView>
<StackLayout>
<CollectionView x:Name="TermsList"
ItemSizingStrategy="MeasureAllItems">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0"
Grid.Row="0"
Text="Term Name:"
TextColor="{StaticResource PrimaryBlue}"
FontFamily="{StaticResource FuturaStdMedium}"/>
<Entry Grid.Column="1"
Grid.Row="0"
Text="{Binding TermName}"
TextColor="{StaticResource SecondaryBlue}"
FontFamily="{StaticResource FuturaStdMedium}"/>
<Label Grid.Column="0"
Grid.Row="1"
Text="Start Date:"
TextColor="{StaticResource PrimaryBlue}"
FontFamily="{StaticResource FuturaStdMedium}"/>
<DatePicker MinimumDate="01/01/2020"
MaximumDate="12/31/2050"
Date="{Binding StartDate}"
DateSelected="OnDateSelected"
Grid.Column="1"
Grid.Row="1"
TextColor="{StaticResource SecondaryBlue}"
FontFamily="{StaticResource FuturaStdBook}">
<DatePicker.Format>MM/dd/yyyy</DatePicker.Format>
</DatePicker>
<Label Grid.Column="0"
Grid.Row="2"
Text="End Date:"
TextColor="{StaticResource PrimaryBlue}"
FontFamily="{StaticResource FuturaStdMedium}"/>
<DatePicker MinimumDate="02/01/2020"
MaximumDate="01/01/2051"
Date="{Binding EndDate}"
DateSelected="OnDateSelected"
Grid.Column="1"
Grid.Row="2"
TextColor="{StaticResource SecondaryBlue}"
FontFamily="{StaticResource FuturaStdBook}">
<DatePicker.Format>MM/dd/yyyy</DatePicker.Format>
</DatePicker>
<Button Text="View Courses for Term"
Grid.ColumnSpan="2"
Grid.Row="3"
x:Name="CoursesButton"
Clicked="CoursesButton_Clicked"/>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<Button Text="Delete Term"
BackgroundColor="Red"
Margin="30"
TextColor="Black"
x:Name="DeleteTermButton"
Clicked="DeleteTermButton_Clicked"/>
</StackLayout>
</ContentPage>
Screenshot:
You could set the width of each Column as 1/2 of screen width
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.5*"/>
<ColumnDefinition Width="0.5*"/>
</Grid.ColumnDefinitions>
add HorizontalOptions="FillAndExpand" in the <Grid>Element inside your collectionView

Xamarin.Forms: issues positioning labels and setting row height?

My xaml has 6 labels that display data. I'm trying to set them, but I'm having trouble doing so. This is how the form should be rendered:
Here's what I've tried so far. Here's the xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:maps="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps"
xmlns:local="clr-namespace:GasStations"
x:Class="GasStations.MainPage">
<Grid RowSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="50" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<StackLayout Grid.Row="0" x:Name="MapGrid">
<maps:Map WidthRequest="960" HeightRequest="200"
x:Name="MyMap" IsShowingUser="true"/>
</StackLayout>
<StackLayout Grid.Row="1">
<Button Text="Show list" x:Name="Button_DisplayList"
VerticalOptions="CenterAndExpand"
HorizontalOptions="Center"
Clicked="OnButtonClicked" />
</StackLayout>
<StackLayout Grid.Row="2" x:Name="listSection" IsVisible="false" HeightRequest="200">
<ListView x:Name="ListView_Pets">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="10,10,10,10">
<Grid.RowDefinitions>
<RowDefinition Height="300"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackLayout BackgroundColor="#2FA4D9" Grid.Row="0" Grid.Column="0" HeightRequest="300" WidthRequest="200">
<Label Text="{Binding Name}" FontSize="15" TextColor="White" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
<Label Text="{Binding Description}" FontSize="15" TextColor="White" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
<Label Text="{Binding Lon}" FontSize="15" TextColor="White" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
<Label Text="{Binding Lat}" FontSize="16" TextColor="White" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
</StackLayout>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</Grid>
</ContentPage>
This is the result. It clearly doesn't look anything like the image above:
Besides all the issues with the labels, what doesn't seem to change is the row height: <RowDefinition Height="300"> doesn't change anything, and neither does HeightRequest="200".
Any help is appreciated.
The ViewCell definition you have defines a Grid with a single cell inside it (one row and one column. From the UI example you have, it looks more like what you want is 2 columns and 3 rows, something like this:
<ViewCell>
<Grid Padding="10,10,10,10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Text="{Binding Name}" FontSize="15" TextColor="Black" Grid.Row="0" Grid.Column="0"/>
<Label Text="{Binding Description}" FontSize="15" TextColor="Black" Grid.Row="1" Grid.Column="0"/>
<Label Text="{Binding Lon}" FontSize="15" TextColor="White" BackgroundColor="#2FA4D9" Grid.Row="0" Grid.Column="1"/>
<Label Text="{Binding Lat}" FontSize="16" TextColor="White" BackgroundColor="#2FA4D9" Grid.Row="1" Grid.Column="1"/>
</Grid>
</ViewCell>
This is based on the code sample you have, so won't completely implement the UI design, but should get closer. The key is to have the Labels in their own cell in the Grid rather than having them all in a single StackLayout.
You will also likely need to set the RowHeight (as Jason noted) or set HasUnevenRows="true" on the ListView.

Uneven space after last row of the grid inside listview in xamarin.forms

I am working in xamarin.forms. I am facing the very silly issue and already wasted 5 hours on it. It's designing issue.
<Grid x:Name="MaingrdForms">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ListView Grid.Row="0" x:Name="lstLatestProducts" HasUnevenRows="True"
ItemAppearing="LstLoadmore"
IsPullToRefreshEnabled="true"
Refreshing="LstPulled"
CachingStrategy="RecycleElement">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="0.5*"/>
</Grid.ColumnDefinitions>
<Image Grid.Row="0" Grid.Column="0" Grid.RowSpan="3" HeightRequest="100" WidthRequest="100"
Source="{Binding ProductImg}" Margin="5" VerticalOptions="Center" HorizontalOptions="Center" />
<Label Grid.Row="0" Grid.Column="1" Text="{Binding ProductName}" Style="{StaticResource LabelHeaderStyle}"/>
<Label Grid.Row="1" Grid.Column="1" Text="{Binding ProductDesc}" Style="{StaticResource LabelDescStyle}"/>
<StackLayout Grid.Row="2" Grid.Column="1" Orientation="Horizontal">
<Image Source="icon_password.png"/>
<Label Text="{Binding SubscriptionCategory}" VerticalOptions="Center" HorizontalOptions="Start"/>
<Image Source="icon_password.png"/>
<Label Text="{Binding CreatedDate}" VerticalOptions="Center" HorizontalOptions="Start"/>
</StackLayout>
<Image Grid.Row="0" Grid.Column="2" Grid.RowSpan="3" VerticalOptions="Start" Source="erecall.png"/>
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Label Grid.Row="0" x:Name="lblNoRecordFound" Text="No Record found" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"/>
</Grid>
In List, I am taking HasUnevenRow=true to set row height but in every row, there is some space after the last row. the height of that space is also different. I can't figure out where is the actual issue.

Categories

Resources