<StackLayout HeightRequest="150">
<listView:SfListView x:Name="listView" ItemSize="90" ItemsSource="{Binding contactsinfo}"
ItemSpacing="0,0,5,5" HeightRequest="150"
SelectionMode="None" Orientation="Horizontal">
<listView:SfListView.ItemTemplate>
<DataTemplate>
<Grid RowSpacing="1" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
<Label LineBreakMode="NoWrap" TextColor="#474747" Text="{Binding ContactName}"
Grid.Row="1" HorizontalTextAlignment="Center" VerticalTextAlignment="Center">
</Grid>
</DataTemplate>
</listView:SfListView.ItemTemplate>
</listView:SfListView>
</StackLayout> |
<listView:SfListView x:Name="listView" ItemSize="200" ItemsSource="{Binding contactsinfo}"
ItemSpacing="0,0,0,0"
Orientation="Horizontal" BackgroundColor="Green">
<listView:SfListView.ItemTemplate>
<DataTemplate>
<Grid HeightRequest="300" WidthRequest="300" HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand" BackgroundColor="Blue">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Source="{Binding ContactImage}" HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand"/>
</Grid>
</DataTemplate>
</listView:SfListView.ItemTemplate>
</listView:SfListView> |
Hi, it is old post indeed but I have tried your first sample with the newest nuget packages and XF 5 and I am not able to center horizontal listview within the Stacklayout. It always aligns on left side. I have tried to set "CenterAndExpand" on listview as well as on DataTemplate. Has something been changed in between and this not working any longer?
Just to make clear, I want to align listview itself in the center and expand it.
So basically if there is 1 item, it should be in the center.
If there are 10 items it should fill the horizontal space. Will that work?
private void listView_Loaded(object sender, ListViewLoadedEventArgs e)
{
var scrollView = listView.Children[0] as ExtendedScrollView;
var itemsSource = listView.ItemsSource as ObservableCollection<BookInfo>;
if (itemsSource != null && itemsSource.Count > 0 && itemsSource.Count < 3)
{
listView.ItemSize = scrollView.Width / itemsSource.Count;
}
} |