I need to bind an ObservableCollection view to a FlexLayout (because I need a custom appearance). When I bind the items to the CollectionView they don't have same look that I get when I use grid directly inside FlexLayout, for example:
This works as expected but with no binding, of course.
<FlexLayout Grid.Row="5"
Grid.Column="0"
Margin="10,15,10,5"
HorizontalOptions="FillAndExpand"
VerticalOptions="StartAndExpand">
<Grid Margin="0,0,0,6"
HeightRequest="20">
<Frame Padding="2"
BackgroundColor="#f1f1f1"
CornerRadius="5">
<Label Grid.Row="0"
Grid.Column="1"
FontSize="11"
Text="some text"
TextColor="DarkGray" />
</Frame>
</Grid>
</FlexLayout>
Here everything is binding ok but the appearance is like a ListView:
in code behind:
xEspecialidades.ItemsSource = StringCollection;
in XAML:
<FlexLayout Grid.Row="5"
Grid.Column="0"
Margin="10,15,10,5"
HorizontalOptions="FillAndExpand"
VerticalOptions="StartAndExpand">
<CollectionView x:Name="xEspecialidades"
x:FieldModifier="public static"
HorizontalOptions="FillAndExpand"
HorizontalScrollBarVisibility="Never"
VerticalOptions="StartAndExpand"
VerticalScrollBarVisibility="Never">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid Margin="0,0,0,6"
HeightRequest="20">
<Frame Padding="2"
BackgroundColor="#f1f1f1"
CornerRadius="5">
<Label Grid.Row="0"
Grid.Column="1"
FontSize="11"
Text="{Binding .}"
TextColor="DarkGray" />
</Frame>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</FlexLayout>
I've found this thread https://github.com/xamarin/Xamarin.Forms/issues/8234 but could not make it work. Visual Studio keeps saying that FlexItemsLayout is not found. Is there a way to do what I want?
Got it working by using code below:
<FlexLayout x:Name="xEspecialidades"
BindableLayout.ItemsSource="{Binding especialidades}"
HorizontalOptions="FillAndExpand"
VerticalOptions="StartAndExpand">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Grid Margin="0,0,0,6"
HeightRequest="20"
HorizontalOptions="StartAndExpand">
<Frame Padding="2"
BackgroundColor="#f1f1f1"
CornerRadius="5"
HorizontalOptions="StartAndExpand">
<Label Grid.Row="0"
Grid.Column="1"
FontSize="11"
HorizontalOptions="StartAndExpand"
Text="{Binding .}"
TextColor="DarkGray" />
</Frame>
</Grid>
</DataTemplate>
</BindableLayout.ItemTemplate>
</FlexLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With