Change grid layout at runtime - xamarin.forms

I have a grid with the following row definitions...
<Grid.RowDefinitions>
<RowDefinition Height="5*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="2*" />
</Grid.RowDefinitions>
When the user presses a button, how do I remove the bottom two rows and have the top row (including the view within it) stretch to the bottom of the grid?
I kept all the rows and did this...
mainGrid.RowDefinitions[2].Height = new GridLength(1, GridUnitType.Star);
mainGrid.ForceLayout();
I would like to animate the change in height.

Try something like this:
The grid
<Grid x:Name="grid">
<Grid.RowDefinitions>
<RowDefinition Height="5*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="2*" />
</Grid.RowDefinitions>
<StackLayout Grid.Row="0"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
x:Name="stkOne"
BackgroundColor="Red"/>
<StackLayout Grid.Row="1"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
x:Name="stkTwo"
BackgroundColor="Green"/>
<StackLayout Grid.Row="1"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
x:Name="stkThree"
BackgroundColor="Blue"/>
<Button Text="Expand One"
HorizontalOptions="EndAndExpand"
Command="ExpandCommand"/>
</Grid>
The code behind
// Create the command and set it up to execute this method:
void ExecuteExpand()
{
stkTwo.IsVisible=false;
stkThree.IsVisible=false;
Grid.SetRowSpan(stkOne, 3);
grid.ForceLayout();
}
I'm not sure if it will work as you need, but make a try.

Related

After translating a view it should scroll the full content and if not it should crop the content in the hidden part

This query is the continuation of this post Scroll view scrolling not works when the it is translated in y position such that bottom part gets hidden in xamarin forms
Now scroll view works fine, in some cases I used to define like below, I can either use the content of the absolute layout with scroll view or else directly.
When used directly as in below snippet, after translation by setting layout bounds, all the box view were arranged in the specified height.
But previously it will crop at the bottom after translation. I need the same behavior when scroll view is not used. How to achieve this.
<AbsoluteLayout x:Name="layout" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Grid.Row="1">
<Grid x:Name="scrollView" AbsoluteLayout.LayoutFlags="SizeProportional" AbsoluteLayout.LayoutBounds="0,0,1,1" >
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<BoxView Grid.Row="0" BackgroundColor="Red"></BoxView>
<BoxView Grid.Row="1" BackgroundColor="Black"></BoxView>
<BoxView Grid.Row="2" BackgroundColor="Blue"></BoxView>
<BoxView Grid.Row="3" BackgroundColor="Gray"></BoxView>
<BoxView Grid.Row="4" BackgroundColor="Green"></BoxView>
<BoxView Grid.Row="5" BackgroundColor="Red"></BoxView>
<BoxView Grid.Row="6" BackgroundColor="Black"></BoxView>
<BoxView Grid.Row="7" BackgroundColor="Blue"></BoxView>
<BoxView Grid.Row="8" BackgroundColor="Gray"></BoxView>
<BoxView Grid.Row="9" BackgroundColor="Green"></BoxView>
</Grid>
In this case When I set layout bounds it gets arranged as in second image, but I need as in first image.
If you don't have ScrollView, like you had in this question then just put 1 as a last parameter of your Rectangle constructor.
I don't know how the rest of your code looks like, but you can bind different rectangles with different AbsoluteLayout.LayoutBounds. For example, in a case without ScrollView you can put like this
<AbsoluteLayout x:Name="layout" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Grid.Row="1">
<Grid AbsoluteLayout.LayoutBounds="{Binding rectNoScroll}"...
rectNoScroll = new Rectangle(0, 250, 1, 1);
and when you have ScrollView you can do like in here.

Xamarin.Forms: Add item to certain row of grid from code

in my XAML I have this grid:
<Grid Grid.Row="12" Grid.Column="1" ColumnSpacing="0" >
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<!--Label-->
<RowDefinition Height="3*" ></RowDefinition>
<!--ProfilePic-->
<RowDefinition Height="*"></RowDefinition>
<!-- Btn change-->
</Grid.RowDefinitions>
<BoxView BackgroundColor="Purple" Grid.Row="1"></BoxView>
<ImageButton
Grid.Row="2"
x:Name="row_forpic_editProfile"
HorizontalOptions="Center"
VerticalOptions="Center"
/>
<Label
Grid.Row="0"
FontSize="16"
TextColor="#272727"
VerticalOptions="End"
Text="Proilfbild"
FontFamily="arial"
/>
<Button
Grid.Row="2"
x:Name="btn_changepic_editprofile"
Text="Ändern"
HorizontalOptions="Center"
VerticalOptions="Center"
/>
</Grid>
This is the result of that:
The result
Now, I want to add an imageview into the purple row (which is Grid.Row="1") from code because I downloaded a picture from the internet.
How can I now add my new Image (ImageButton) into this exact row? (row 1)
And also have it maintain its dimensions (only the picture must full the container).
thank you:)
to add a View to a Grid
myGrid.Children.Add(view,col,row);

Can you define RowDefinition in style xamarin forms

I have the following
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="1.5*"></RowDefinition>
<RowDefinition Height="7.5*"></RowDefinition>
</Grid.RowDefinitions>
and I would like to define the Height in a Style or in a variable in the app.xaml.
<Grid.RowDefinitions>
<RowDefinition Height="{StaticResource myHeight}"></RowDefinition>
<RowDefinition Height="{StaticResource myHeight2}"></RowDefinition>
</Grid.RowDefinitions>
Is this possible? I have tried but I get an error.
The reason I want to achieve this is that on small devices I need to change the row height.
I would appreciate any tips or suggestions.
You can bind a model to ContentPage , then Grid can use binded data by model and row height can be setted by model data (contain row height paramaters).
For example , create a sample ViewModel(RowHeightClass) as follow :
using Xamarin.Essentials;
public class RowHeightClass
{
public GridLength rowFirstHeight { set; get; }
public GridLength rowSecondHeight { set; get; }
public RowHeightClass()
{
// here can add code set by device screen
var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
double screenheight = mainDisplayInfo.Height;
rowFirstHeight = new GridLength(DeviceDisplay * 0.2);
rowSecondHeight = new GridLength(DeviceDisplay * 0.4);
}
}
Binding it in ContentPage :
RowHeightClass rowHeightClass = new RowHeightClass();
BindingContext = rowHeightClass;
Used in XAML :
<Grid VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="{Binding rowFirstHeight}" />
<RowDefinition Height="{Binding rowSecondHeight}" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<BoxView Grid.Row="0"
Grid.Column="0"
BackgroundColor="Blue" />
<BoxView Grid.Row="1"
Grid.Column="0"
BackgroundColor="Red" />
<Button Grid.Row="2"
Grid.Column="0"
BackgroundColor="Yellow"
Clicked="Button_Clicked" />
</Grid>
Note : You can modify the constructor method of RowHeightClass to add method to set needed height by the size of device screen .
==================================Update=====================================
Using StaticResource also can set the heigh of row . Add a Resources for Grid or Application as follow :
<Grid VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand">
<Grid.Resources>
<ResourceDictionary>
<x:Double x:Key="SmallScreenRowheight">200</x:Double>
<x:Double x:Key="LargeScreenRowheight">400</x:Double>
</ResourceDictionary>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="{StaticResource SmallScreenRowheight}" />
<RowDefinition Height="{StaticResource LargeScreenRowheight}" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<BoxView Grid.Row="0"
Grid.Column="0"
BackgroundColor="Blue" />
<BoxView Grid.Row="1"
Grid.Column="0"
BackgroundColor="Red" />
<Button Grid.Row="2"
Grid.Column="0"
BackgroundColor="Yellow"
Clicked="Button_Clicked" />
</Grid>

Frame not taking the full height Xamarin forms

I have Xamarin page which has a grid with two rows and a column. In the first row/column I have a label and the second row/column I have a frame where it does not take the full height of the row. Could you help, please?
<controls:GradientColorStack
StartColor="#0cacee"
EndColor="#0574d8"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
GradientOreintation="Vertical">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="330" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Grid.Column="0"
Grid.Row="0"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Text="" />
<controls:CustomFrame
Grid.Row="1"
CornerRadius="30,30,0,0"
BackgroundColor="White"
HasShadow="True"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Margin="12,20,12,0" />
</Grid>
</controls:GradientColorStack>
There is default spacing in Row and Column .
Grid has properties to control spacing between rows and columns. The following properties are available for customizing the Grid:
ColumnSpacing – the amount of space between columns. The default value of this property is 6.
RowSpacing – the amount of space between rows. The default value of this property is 6.
Here you can set RowSpacing= "0" to get full height of the row.
<Grid BackgroundColor="Accent" RowSpacing="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="330"/>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label BackgroundColor="Wheat" Grid.Column="0"
Grid.Row="0"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Text="1111111111" />
<Frame
Grid.Row="1"
BackgroundColor="White"
HasShadow="True"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Margin="12,0,12,0" />
</Grid>
By the way Margin="12,20,12,0" also can affect the top space, if not necessary ,can set this : Margin="12,0,12,0"
Works fine with frames ?
<Frame
BackgroundColor="Red"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40" />
<RowDefinition Height="370" />
</Grid.RowDefinitions>
<Label Grid.Column="0"
BackgroundColor="GREEN"
Grid.Row="0"
HorizontalOptions="FillAndExpand"
VerticalOptions="Start"
Text="LOL" />
<Frame
Grid.Row="1"
Grid.Column="0"
BackgroundColor="White"
HasShadow="True"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Margin="12,20,12,0">
<StackLayout>
<Label Text="Text" />
</StackLayout>
</Frame>
</Grid>
</Frame>

CustomView not rendered if I put inside a grid with RowDefinitions in Xamarin.Forms

I have loaded the views inside a grid as below,
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Entry x:Name="SearchTextBox"
Grid.Row="0"
HorizontalOptions="Fill"
Placeholder="Search"
VerticalOptions="Fill" />
<ListView x:Name="listView"
Grid.Row="1"
HorizontalOptions="Fill"
VerticalOptions="Fill" />
<local:CustomListView x:Name="customView"
Grid.Row="0"
ItemsSource="{Binding Items}">
</local:CustomListView >
</Grid>
Out of 3 views in the above code snippet, first two views are rendered properly. But the 3rd view(custom view) not rendered. CustomViews are not rendered only if I set RowDefinition as "Auto". So, anyone please tell why the custom view is not loaded if I set "Auto".
your CustomListView probably doesn't request a minimum size correctly, and as the middle row has a height of *, it takes all of the available space minus the minimum size requests.

Resources