Starting in Xamarin.Forms 4.0 there is a new element called the CollectionView
. According to the Xamarin team, this new element should replace the well known control ListView
.
The CollectionView is intended to be a successor to the ListView, improving upon its design by reducing technical complexity and allowing for more flexibility of layout and function
I recently started to look at the CollectionView
(in version 4.0.0.169046-pre5), and noticed one thing missing: item spacing. From my experience, the CollectionView
seems to ignore the Margin
or Padding
properties on its elements. The good news is that the team is aware of this, and there is an issue to track it: https://github.com/xamarin/Xamarin.Forms/issues/4681
In the meantime, there is a pretty simple work around. Wrap the content in an additional ContentView
, and the Padding
/ Margin
will then worked as expected.
<CollectionView
ItemsSource="{Binding Items}">
<CollectionView.ItemTemplate>
<DataTemplate>
<ContentView> <!-- Notice the additional ContentView here -->
<Frame
HeightRequest="100"
HorizontalOptions="StartAndExpand"
VerticalOptions="StartAndExpand"
Margin="10, 5"
Padding="10"
IsClippedToBounds="True">
<Grid>
...
One shortcoming of this workaround is that when you click / tap on an item, the entire ContentView
will be highlighted, but that Effect
or Renderer
is for another post.