First item is wider than the rest in collectionview xamarin forms - xaml

I'm trying to use CollectionView with a gridlayout. But the first item of the list always becomes wider than the rest. I even tried by giving specific widthrequest. Thanks in advance!!!
<Grid Margin="0">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="340"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<CollectionView Margin="0" ItemsSource="{Binding Projects}"
ItemSizingStrategy="MeasureFirstItem"
Grid.Row="0" Grid.Column="1">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical"
Span="3"
VerticalItemSpacing="20"
HorizontalItemSpacing="20"/>
</CollectionView.ItemsLayout>
<CollectionView.Footer>
<BoxView HeightRequest="0"/>
</CollectionView.Footer>
<CollectionView.ItemTemplate>
<DataTemplate>
<BoxView Margin="0" BackgroundColor="Blue"
HeightRequest="100" WidthRequest="100"/>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</Grid>

The issue couldnt be resolved. So, I removed the gridlayout and replaced it with nested collectionview. Thanks for reaching out everyone.
<Grid Margin="0">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="380"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<CollectionView Margin="10" ItemsSource="{Binding ProjectList}" Grid.Row="0" Grid.Column="1">
<CollectionView.ItemsLayout>
<LinearItemsLayout Orientation="Vertical"
ItemSpacing="30" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout WidthRequest="100" Margin="0" Padding="0" Spacing="0">
<CollectionView
ItemsSource="{Binding RowOfProjects}"
Margin="0"
HeightRequest="100" HorizontalScrollBarVisibility="Never"
VerticalOptions="CenterAndExpand">
<CollectionView.ItemsLayout>
<LinearItemsLayout Orientation="Horizontal"
ItemSpacing="30" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
....
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</Grid>

I recommend you to set the HeightRequest and/or WidthRequest in the first grid inside the DataTemplate
<CollectionView>
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid HeightRequest="300">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="340"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<CollectionView Margin="0" ItemsSource="{Binding Projects}"
ItemSizingStrategy="MeasureFirstItem"
Grid.Row="0" Grid.Column="1">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical"
Span="3"
VerticalItemSpacing="20"
HorizontalItemSpacing="20"/>
</CollectionView.ItemsLayout>
<CollectionView.Footer>
<BoxView HeightRequest="0"/>
</CollectionView.Footer>
<CollectionView.ItemTemplate>
<DataTemplate>
<BoxView Margin="0" BackgroundColor="Blue"
HeightRequest="100" WidthRequest="100"/>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
This example can also help you: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/layout#vertical-grid

Related

Xamarin.Forms XAML How to show columns in center of grid (not left bind)

I have this grid object:
<!--Legende-->
<Grid Grid.Row="0">
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.8*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="0.8*"/>
</Grid.RowDefinitions>
<Grid Grid.Column="0" Grid.Row="1" HorizontalOptions="Center">
<BoxView BackgroundColor="#faea57" CornerRadius="25"></BoxView>
<Label
Padding="10,0,10,0"
VerticalOptions="Center"
Grid.Column="0"
Text="Geplant"
FontSize="14"
FontFamily="Galvji-01.ttf"
TextColor="#ffffff"/>
</Grid>
<Grid Grid.Column="1" Grid.Row="1" HorizontalOptions="Center">
<BoxView BackgroundColor="#75f056" CornerRadius="25"></BoxView>
<Label
Padding="10,0,10,0"
VerticalOptions="Center"
Text="Aktiv"
FontSize="14"
FontFamily="Galvji-01.ttf"
TextColor="#ffffff"/>
</Grid>
<Grid Grid.Column="2" Grid.Row="1" HorizontalOptions="Center">
<BoxView BackgroundColor="#fa594d" CornerRadius="25"></BoxView>
<Label
Padding="10,0,10,0"
VerticalOptions="Center"
Text="Abgeschlossen"
FontSize="14"
FontFamily="Galvji-01.ttf"
TextColor="#ffffff"/>
</Grid>
</Grid>
The result is this:
The 3 boxviews with the text are bind to the left ob the screen. However, I want those three objets to be rendered in the CENTRE.
I cannot work with padding, since the amount of padding varies from phone to phone so I have to work with attributes like "center", but I cannot seem to figure out how to do that.
Help would be awesome!
Thanks
You could set two extra space column to get the effect like padding.
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.2*"/> // left
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="0.2*"/> // right
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.8*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="0.8*"/>
</Grid.RowDefinitions>
<Grid Grid.Column="1" Grid.Row="1" HorizontalOptions="Center">
<BoxView BackgroundColor="#faea57" CornerRadius="25"></BoxView>
<Label
Padding="10,0,10,0"
VerticalOptions="Center"
Grid.Column="0"
Text="Geplant"
FontSize="14"
FontFamily="Galvji-01.ttf"
TextColor="#ffffff"/>
</Grid>
<Grid Grid.Column="2" Grid.Row="1" HorizontalOptions="Center">
<BoxView BackgroundColor="#75f056" CornerRadius="25"></BoxView>
<Label
Padding="10,0,10,0"
VerticalOptions="Center"
Text="Aktiv"
FontSize="14"
FontFamily="Galvji-01.ttf"
TextColor="#ffffff"/>
</Grid>
<Grid Grid.Column="3" Grid.Row="1" HorizontalOptions="Center">
<BoxView BackgroundColor="#fa594d" CornerRadius="25"></BoxView>
<Label
Padding="10,0,10,0"
VerticalOptions="Center"
Text="Abgeschlossen"
FontSize="14"
FontFamily="Galvji-01.ttf"
TextColor="#ffffff"/>
</Grid>
</Grid>

Xamarin Forms Center Grid horizontal

I have a problem. I am trying to achieve the following Grid:
To do that, I thought that I should set both the ColumnWidth to Auto and set the HorizontalOptions of the Grid to Center, so the Grid is centred in the middle of the screen creating equal spaces at the left and right side, but no matter what I try, I can't seem to move the columns from the right side. Here is what I have now:
And here is my code:
<Frame BorderColor="#00D8FF" Padding="0" HorizontalOptions="FillAndExpand" BackgroundColor="Transparent">
<StackLayout Orientation="Vertical">
<CarouselView ItemsSource="{Binding coinDataList}" HeightRequest="50">
<CarouselView.ItemTemplate>
<DataTemplate>
<Grid RowSpacing="0" HorizontalOptions="Center">
<Grid.RowDefinitions>
<RowDefinition Height="25" />
<RowDefinition Height="25" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="1" Text="BTC-USDT" FontAttributes="Bold" TextColor="#00D8FF" FontSize="18"/>
<Label Grid.Row="1" Grid.Column="1" Text="9762.33" TextColor="White" FontSize="18"/>
<Label Grid.RowSpan="2" Grid.Column="2" Text="-$476.22 (-4.77%)" VerticalOptions="CenterAndExpand" FontAttributes="Bold" TextColor="Red" FontSize="18" Margin="15,0,0,0"/>
</Grid>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
</StackLayout>
</Frame>
How can I create my wanted result?
Can you try this?
<Frame BorderColor="#00D8FF" Padding="0" HorizontalOptions="FillAndExpand" BackgroundColor="Transparent">
<StackLayout Orientation="Vertical">
<CarouselView ItemsSource="{Binding coinDataList}" HeightRequest="50">
<CarouselView.ItemTemplate>
<DataTemplate>
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="1" Text="BTC-USDT" FontAttributes="Bold" TextColor="#00D8FF" FontSize="18"/>
<Label Grid.Row="1" Grid.Column="1" Text="9762.33" FontAttributes="Bold" TextColor="#00D8FF" FontSize="18"/>
<Label Grid.Row="0" Grid.Column="2" VerticalTextAlignment="Center" Text="-$476.22 (-4.77%)" FontAttributes="Bold" TextColor="#00D8FF" FontSize="18"/>
</Grid>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
</StackLayout>
</Frame>

Xamarin Forms Access parent collection item

I have a problem. I have a CollectionView with a List<Album> albumList with inside an Album a List<Picture> Pictures. Now I created the following XAML:
<CollectionView ItemsSource="{Binding albumList}">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid VerticalOptions="Center">
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="90"/>
<RowDefinition Height="5"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="30"/>
<RowDefinition Height="10"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="3"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="3"/>
</Grid.ColumnDefinitions>
<StackLayout Grid.Row="3" Grid.Column="3" Grid.ColumnSpan="2" Padding="0" Margin="0"
BindableLayout.ItemsSource="{Binding Pictures}" Orientation="Vertical">
<BindableLayout.ItemTemplate>
<DataTemplate>
<StackLayout Orientation="Horizontal">
<Label Grid.Column="1" Text="{Binding albumList.Price, StringFormat='€ {0:F2}'}" TextColor="Black" FontSize="16" />
</StackLayout>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
Now I want to access the current Album.Price inside the following line: <Label Grid.Column="1" Text="{Binding albumList.Price, StringFormat='€ {0:F2}'}" TextColor="Black" FontSize="16" />
This is outside the current binding of Pictures, so how can I enter a value from outside the current Binding?
You can define the BindingContext from a reference of an element outside the nested Collection, give a name to your StackLayout for example:
<StackLayout x:Name="ElementForReference" Grid.Row="3" Grid.Column="3" Grid.ColumnSpan="2" Padding="0" Margin="0"
BindableLayout.ItemsSource="{Binding Pictures}" Orientation="Vertical">
And then define the BindingContext with the reference of the element outside the nested one:
{Binding BindingContext.Price, Source={x:Reference Name=ElementForReference}}

I can list my messages , Listview

I can list my messages, but now I wanted every message after that I had a balloon. To identify if I sent or received. The balloon is now the text is not staying. Can you help me?
<ListView x:Name="Post_List" Grid.Row="1">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid ColumnSpacing="2" Padding="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="40"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Frame Grid.Row="0" OutlineColor="Transparent"
HasShadow="False" Grid.Column="0"
CornerRadius="5" BackgroundColor="#03A9F4">
<Label TextColor="White" Text="{Binding Mensagem}"/>
</Frame>
<Label FontSize="Micro" Grid.Row="1" Grid.Column="0"
Text="{Binding Data}" TextColor="Gray"></Label>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Adding HasUnevenRows="True" to your ListView will fix your issue.
This is because you did not set any RowHeight which is alright cause you want the height to be based on the content but the ListView does not know the dimension to apply. Using the HasUnevenRows the ListView will be calculating the size to fit the content.
<ListView x:Name="Post_List" Grid.Row="1" HasUnevenRows="True" >
<ListView.ItemTemplate >
<DataTemplate >
<ViewCell>
<Grid ColumnSpacing="2" Padding="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="40" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Frame Grid.Row="0" OutlineColor="Transparent" HasShadow="False" Grid.Column="0" CornerRadius="5" BackgroundColor="#03A9F4">
<Label TextColor="White" Text="{Binding Mensagem}"/>
</Frame>
<Label FontSize="Micro" Grid.Row="1" Grid.Column="0" Text="{Binding Data}" TextColor="Gray"></Label>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Hope this helps.-

Stackpanels side by side

I want to put stackPanels side by side. Then in each stack panel, there are different controls. Now the first stack panel is working. It has textblocks and text boxs. Now I want to add a button on the second stack panel and so on. The question is that the second panel doesn't show the button's content. Not sure why?
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal" Width="300" HorizontalAlignment="Left" Margin="10">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="100"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition Height="25"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Text="Year" TextAlignment="Center"></TextBlock>
<TextBlock Grid.Column="1" Grid.Row="0" Text="Week" TextAlignment="Center"></TextBlock>
<TextBlock Grid.Column="2" Grid.Row="0" Text="File Location" TextAlignment="Center"></TextBlock>
</Grid>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="0" Margin="10">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="100"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Button Grid.Column="0" Grid.Row="0">
<TextBlock Text="Get Informations" TextWrapping="Wrap" TextAlignment="Center"></TextBlock>
</Button>
</Grid>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Column="2" Grid.Row="0"></StackPanel>
<StackPanel Orientation="Horizontal" Grid.Column="0" Grid.Row="1"></StackPanel>
<StackPanel Orientation="Horizontal" Grid.Column="2" Grid.Row="1"></StackPanel>
</Grid>
</StackPanel>
</StackPanel>
You are setting the width of the Stackpanel as 300, if you want a solution. either remove the stackpnael (the one that has 300 width) or fix the Grid inside that stackpanel to be 300