It’s been a little while since I’ve posted about any new Xamarin Controls or updates, but here’s a great one!
I needed to create a layout that could bind to a collection of data in Xamarin.Forms that fit to a dynamic grid that also scaled to larger devices and orientation changes. I thought about creating a new custom renderer that uses the RecyclerView
with a GridLayoutManager
and a UICollectionView
with a custom layout. However, I didn’t need to bind to tons of data, so I decided to create a new control that is entirely Xamarin.Forms – No custom renderers!
I’ve called it the DynamicWrapLayout
since it was inspired by the WrapLayout
from Charles Petzold and David Britch.
Get it on NuGet: https://www.nuget.org/packages/DynamicWrapLayout/
Get the source on GitHub: https://github.com/SuavePirate/DynamicWrapLayout
Because it is all Xamarin.Forms, it should work on all the platforms that Forms supports! So use it everywhere! I’ll create another post about how I built it if you want to work on your own implementation, but for now, you can read the documentation on the control below.
Documentation
DynamicWrapLayout
A Xamarin.Forms layout for creating dynamically wrapped views. Inspired by the WrapLayout
example: https://developer.xamarin.com/samples/xamarin-forms/UserInterface/CustomLayout/WrapLayout/
Installation
It’s on NuGet! https://www.nuget.org/packages/DynamicWrapLayout/
Install-Package DynamicWrapLayout
Be sure to install in all projects that use it.
Usage
There are two key properties that make this control useful – the ItemsSource
(like a ListView
) and the DataTemplate
(although, you can also just add children to the view – it does both!)
Be sure to wrap it in a ScrollView
though
XAML
Add the xmlns
:
xmlns:suave="clr-namespace:SuaveControls.DynamicWrapLayout;assembly=SuaveControls.DynamicWrapLayout"
Use it in your View:
<ScrollView>
<suave:DynamicWrapLayout ItemsSource="{Binding Items}" HorizontalOptions="Fill">
<suave:DynamicWrapLayout.ItemTemplate>
<DataTemplate>
<StackLayout BackgroundColor="Gray" WidthRequest="120" HeightRequest="180">
<Label Text="{Binding .}" TextColor="White" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" />
</StackLayout>
</DataTemplate>
</suave:DynamicWrapLayout.ItemTemplate>
</suave:DynamicWrapLayout>
</ScrollView>
Don’t like data-binding and want to just use child views? You can do that too!
<ScrollView>
<suave:DynamicWrapLayout HorizontalOptions="Fill">
<StackLayout BackgroundColor="Gray" WidthRequest="120" HeightRequest="180">
<Label Text="0" TextColor="White" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" />
</StackLayout>
<StackLayout BackgroundColor="Gray" WidthRequest="120" HeightRequest="180">
<Label Text="1" TextColor="White" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" />
</StackLayout>
<StackLayout BackgroundColor="Gray" WidthRequest="120" HeightRequest="180">
<Label Text="2" TextColor="White" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" />
</StackLayout>
<StackLayout BackgroundColor="Gray" WidthRequest="120" HeightRequest="180">
<Label Text="3" TextColor="White" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" />
</StackLayout>
<StackLayout BackgroundColor="Gray" WidthRequest="120" HeightRequest="180">
<Label Text="4" TextColor="White" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" />
</StackLayout>
</suave:DynamicWrapLayout>
</ScrollView>
Features
- Bindable child views
- Bindable to collections
- Handles layout changing well (try rotating the device)
- Doesn’t require custom renderers (All Xamarin.Forms baby!)
What does this thing look like?
Android:


iOS:


Notes
This does not use any native view virtualization, which means performance does not scale well with extremely large data sets.
Coming soon
ItemSelected
event and SelectedItem
bindable property (for now, you can add custom gestures and commands to your DataTemplate
and handle the events yourself)
If you like what you see, don’t forget to follow me on twitter @Suave_Pirate, check out my GitHub, and subscribe to my blog to learn more mobile developer tips and tricks!
Interested in sponsoring developer content? Message @Suave_Pirate on twitter for details.
Like this:
Like Loading...