Is it possible to make a collectionview with 4 images per row?
Something like this: Example
Right now all of them are in one row:
<CollectionView x:Name="galleryListView" ItemsSource="{Binding gallerylist}" WidthRequest="500">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Horizontal"/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout WidthRequest="100">
<Image Source="{Binding Image}" ></Image>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
you need to specify how many columns per row
<GridItemsLayout Span="4" Orientation="Horizontal"/>
docs
Related
I have a simple horizontal CollectionView which works fine except that the last item is not showing properly and i have to force scroll it to see it.
I tried all kinds of padding and margins but the only property that will make it show is if make ItemSpacing="20" or bigger. The problem is that, if i added more items, the issue still will show again and this is a dynamic list.
this is the code for the stacklayout under the content page directly.
<StackLayout VerticalOptions="Center" HorizontalOptions="FillAndExpand">
<StackLayout BackgroundColor="LightGray" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" >
<CollectionView HeightRequest="40" VerticalOptions="Fill" HorizontalOptions="Fill" Margin="5" >
<CollectionView.ItemsLayout>
<LinearItemsLayout Orientation="Horizontal" ItemSpacing="10"/>
</CollectionView.ItemsLayout>
<CollectionView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Open (Item)</x:String>
<x:String>Side Menu</x:String>
<x:String>How to?</x:String>
<x:String>Back/Cancel</x:String>
<x:String>Logout</x:String>
<x:String>Yes/No</x:String>
<x:String>App Info</x:String>
</x:Array>
</CollectionView.ItemsSource>
<CollectionView.ItemTemplate>
<DataTemplate>
<Frame BackgroundColor="White" BorderColor="DarkCyan" CornerRadius="20" HasShadow="False" Padding="10" Margin="0">
<Label Text="{Binding}" TextColor="DarkCyan"/>
</Frame>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
</StackLayout>
How to add custom border to the collectionview cell like the image?
<CollectionView HeightRequest="100" ItemsSource="{Binding Test}">
<CollectionView.ItemsLayout >
<GridItemsLayout Orientation="Vertical" Span="4"/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid >
<Image Source="{Binding Image}" ></Image>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
Put the collection view into a colored container could do that, and set margin for the collection view as the width of border, like
<StackLayout BackgroundColor="Black" HorizontalOptions="FillAndExpand">
<CollectionView Margin="4,0">
</CollectionView>
</StackLayout>
I use a sub-class frame to add border, please take a look at the following code:
Creating custom frame in .Net standard library project(shared code).
public class MyFrame : Xamarin.Forms.Frame
{
}
Custom renderer in Android platform.
[assembly: ExportRenderer(typeof(MyFrame), typeof(MyFrameRenderer))]
namespace demo3.Droid
{
[Obsolete]
class MyFrameRenderer : VisualElementRenderer<Frame>
{
public MyFrameRenderer()
{
SetBackgroundDrawable(Resources.GetDrawable(Resource.Drawable.blue_rect));
}
}
}
<ContentPage.Content>
<StackLayout HorizontalOptions="FillAndExpand">
<control:MyFrame Padding="5">
<CollectionView BackgroundColor="Transparent" ItemsSource="{Binding Test}">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical" Span="4" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Frame BackgroundColor="Transparent" BorderColor="Gray">
<Label Text="{Binding .}" TextColor="Black" />
</Frame>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</control:MyFrame>
</StackLayout>
</ContentPage.Content>
The screenshot:
Only items in first row shows different size then other items. However I set same size to all items in XAML. It only happens on Android.
<CollectionView
HorizontalOptions="FillAndExpand"
HeightRequest="100"
ItemsSource="{Binding Items}"
SelectionMode="Single">
<CollectionView.ItemsLayout>
<GridItemsLayout
Orientation="Vertical"
Span="4"
HorizontalItemSpacing="10"
VerticalItemSpacing="10"/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Image
WidthRequest="50"
HeightRequest="50"
Source="{local:ImageResource CollectionView.Image.png}"/>
<Label
Text="{Binding Title}"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center" />
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
Github
I download your sample to test, I can not run your sample, I create new project and upload one sample, you can take a look:
https://github.com/CherryBu/collectionsample1
This is the screenshot:
I want to display list of images as a grid with several rows (2 or 4) with Xamarin Forms. Each cell of the grid must be square. I'm using CollectionView with vertical layout, required span and fixed HeightRequest in DataTemplate. I get multicolumn grid, but I cannot make images (cells) to be squared.
<CollectionView ItemsSource="{Binding .}">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical" Span="2" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Image
HeightRequest="100"
x:Name="imageCell"
Aspect="AspectFill"
Source="{Binding .}" />
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
Thanks to other answers, I've ended up with custom ContentView setting up HeightRequest equal to Width and the image inside it.
public class SquareView : ContentView
{
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
HeightRequest = Width;
}
}
and XAML
<CollectionView
ItemsSource="{Binding .}">
<CollectionView.ItemsLayout>
<GridItemsLayout
Orientation="Vertical"
Span="2" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<views:SquareView>
<Image
HeightRequest="20"
Aspect="AspectFill"
Source="{Binding .}" />
</views:SquareView>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
I've reproduced a small sample.
Only thing I added was to wrap the image inside a contentview, to apply a padding (which you can ignore)
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical" Span="2" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<ContentView Padding="0">
<Image Source="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSzJUY4JZTX9oYsSl1jclFvdsoXhtA0AfxqKkYX2P81Qb3cyX9o"
HeightRequest="150"
Aspect="AspectFill"
/>
</ContentView>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
Don't use <Image></Image> as your root layout in DataTemplate.
Instead, use one of the Layout controls (StackLayout, AbsoluteLayout etc.) or ContentView and then apply some styles or whatever you prefer to this root layout of your CollectionView.
<DataTemplate>
<StackLayout>
<Image
HeightRequest="100"
x:Name="imageCell"
Aspect="AspectFill"
Source="{Binding .}" />
</StackLayout>
</DataTemplate>
Im working on styling up my ListView. In my ItemTemplate are only two Labels in a Stacklayout with Vertical Orientation. As you can see in the Picture there are some spacing between the two Labels. I dont want this space and have the Labels direct under each other. Has someone an advice?
ListView with Label-Background
<ListView x:Name="ItemList"
ItemSelected="ItemList_ItemSelected"
HasUnevenRows="True"
IsGroupingEnabled="true"
GroupDisplayBinding="{Binding LongName}"
GroupShortNameBinding="{Binding ShortName}"
SeparatorVisibility="None">
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Orientation="Horizontal"
BackgroundColor="{StaticResource LightGreyColor}"
Padding="20,10,0,10">
<Label Text="{Binding LongName}"
Style="{DynamicResource DateLabel}"/>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Orientation="Vertical"
Padding="20,10,0,10">
<Label Text="{Binding Name}"
Style="{DynamicResource ItemTitleLabel}"
BackgroundColor="Bisque"/>
<Label Text="{Binding Description}"
Style="{DynamicResource SubTitleLabel}"
BackgroundColor="Bisque"/>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I remember that default spacing between items in StackLayout (and between rows in a Grid) is more than 0. If you want no space between items you can simply add Spacing="0" attribute in your StackLayout.
StackLayout has a default Spacing of 10, you need to set it to 0 if you don't want the spacing between items.