Resposive Grid for Xamarin.Forms not resposive enough - xaml

I'm using a simple grid to make a menu list responsive for all resolutions. It's mainly working fine but if you see it on Android (and UWP if I resize the view to a small width) the right side has less space than the left size which has made it really ugly and unprofessional. Can you help me fix it?
This is the code:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
</Grid>
This is how it looks like:

In Xamarin.Forms, Grid takes Padding by itself so you should set Padding Accordingly OR if you don't want Padding than set Padding=0 .

process of illumination ?:
Could you also change the background colour of your ContentPage to make sure its not applying the margin.
most likely you have something like margin="2, 0, 0, 0" somewhere in your code
<StackLayout Padding="0" Margin="0">
<Grid Padding="0" Margin="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
</Grid>
</StackLayout>

Related

ComboBox in a Grid does not fill a grid's cell width

I have a ComboBox in a Grid along with several text boxes.
The Grid is defined like this:
<Grid Grid.Row="1" Grid.Column="1" Margin="0,10,0,0" ColumnSpacing="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<!-- All of the text boxes -->
<TextBlock Grid.Column="8" Text="Combo" VerticalAlignment="Center" />
<ComboBox Grid.Column="9" Text="{x:Bind ViewModel.ComboText}" ItemsSource="{x:Bind ViewModel.ComboItems}" HorizontalContentAlignment="Stretch" />
</Grid>
Note that I have added in HorizontalContentAlignment. This has no effect in making the combo fill the Grid, and it still looks like this:
How can I make the ComboBox fill the width of the Cell, equally with the other TextBoxes?
ComboBox in a Grid does not fill a grid's cell width
HorizontalContentAlignment is used to make horizontal alignment of the control's content. If you want to make ComboBox fill a gird cell please set HorizontalAlignment as Stretch.
<ComboBox Grid.Column="9"
Text="{x:Bind ViewModel.ComboText}"
ItemsSource="{x:Bind ViewModel.ComboItems}"
HorizontalAlignment="Stretch" />

XAML UWP Adaptive Buttons

I am trying to implement two side-by-side responsive buttons and I want them to behave like on the image below.
This is my XAML:
<RelativePanel x:Name="contentPanel">
<Button x:Name="submitButton" Content="Submit"
Margin="0,40,0,0"
MinWidth="250"
RelativePanel.AlignLeftWithPanel="True"/>
<Button x:Name="annulerButton" Content="Cancel"
Margin="5,40,0,0"
MinWidth="250"
RelativePanel.RightOf="submitButton"
RelativePanel.AlignRightWithPanel="True"/>
<RelativePanel/>
Please, any help?
Use Grid with ColumnDefinition something like below.
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button x:Name="submitButton" Content="Submit" Margin="5" Grid.Column="0" HorizontalAlignment="Stretch" />
<Button x:Name="annuleButton" Content="Cancel" Margin="5" Grid.Column="1" HorizontalAlignment="Stretch"/>
</Grid>
and end output will be
You can use the open-source implementation of the UniformGrid for UWP instead of the relative panel.
Alternatively, you can try to put your buttons into the standard Grid control, though it may misbehave if resized to a rather small size. But as the buttons' minimal width is set to the same value in your case, you will not face this issue.
<Grid x:Name="contentPanel">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button x:Name="submitButton" Content="Submit"
Margin="0,40,0,0"
MinWidth="250"
Grid.Column="0"/>
<Button x:Name="annulerButton" Content="Cancel"
Margin="5,40,0,0"
MinWidth="250"
Grid.Column="1"/>
</Grid>

ListView - Data template and data source in xaml (instant changes in VS designer)

I have something like this:
<ListView >
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<SymbolIcon Symbol="SomeExistingSymbol" Margin="0,0,10,0" />
<TextBlock Grid.Column="1">Some data</TextBlock>
</Grid>
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<SymbolIcon Symbol="SomeExistingSymbol2" Margin="0,0,10,0" />
<TextBlock Grid.Column="1">Some data 2</TextBlock>
</Grid>
<!--More list view items (grids)-->
</ListView>
I don't want to copy Grids over and over. I know I can use binding and data templates.
But I want to have all data defined in xaml - is that possible?
The reason is that I need to see instant changes of data in VS designer, without building project.

Grid columns to be sized by content width but constrained by width of the grid

I have the following markup:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock x:Name="Col1" Text="Text1" Grid.Column="0" />
<TextBlock x:Name="Col2" Text="Text12" Grid.Column="1" />
</Grid>
If I put a lot of text in Col1 it exceeds the screen and Col2 could not be seen at all. I would like to change that behavior such that if there's too much text in Col1 then it's width gets reduced so that Col2 could be fully seen.
On the other hand if I change markup to this:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock x:Name="Col1" Text="Text1" Grid.Column="0" />
<TextBlock x:Name="Col2" Text="Text12" Grid.Column="1" />
</Grid>
Here are images the illustrate how I want TextBlocks to behave:
Combined width of two TextBlocks is smaller than screen so they go one after another.
First Text block is very long so it is cropped in order to contain second TextBlock within screen.
Then everything is OK when Col1 has a lot of text but when the amount of text is small there is a gap between Col1 and Col2. What should I do to get desired behavior in both situations?
Revised;
<Grid HorizontalAlignment="Left">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="Col1" Text="Text1" />
<TextBlock x:Name="Col2" Text="Text12" Grid.Column="1" TextWrapping="Wrap" />
</Grid>
Then Keep in mind to limit their overall size this would be placed in a parent panel container like another Grid

UserControl for a Row in a Grid

I have following Grid:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- GridRow-Definition -->
<Label Grid.Row="0" Grid.Column="0">FirstRow:</Label>
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Binding_To_First_Row}" />
<Label Grid.Row="1" Grid.Column="0">SecondRow:</Label>
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Binding_To_Second_Row}" />
<!-- many more Label-TextBox-Rows -->
</Grid>
Question:
Is there a way to Create a UserControl which contains the Label and TextBox and properly aligns the first Column in a proper way?
The answer is yes, it is possible, but perhaps you should be using a DataGrid or an ItemsControl with a DataTemplate.
The simple answer to your question though is, if you need grid columns in different grids to synchronize their widths, you use the SharedSizeGroup attribute, e.g:
<UserControl>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="column1" Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
</Grid>
</UserControl>
Then in a parent element you use Grid.IsSharedSizeScope="True"
<StackPanel Grid.IsSharedSizeScope="True">
<local:UserControl1/>
<local:UserControl1/>
</StackPanel>
This synchronizes any columns (or rows) that have the same SharedSizeGroup within that scope (you can have multiple nested scopes).