How to Display Selection Background in Xamarin.Forms SfListView?
In Xamarin.Forms SfListView, the item background color will not be displayed for the selected item if the background color is defined for DataTemplate and this is the actual behavior in framework. However, it can be achieved through a work around by defining a property in data model and using a custom converter.
In the model class, define a IsSelected property which indicates whether the item is selected or not and it will have updated when the selection is performed using the SelectionChanged event. Based on this property, you can change the background color of View defined in the ItemTemplate property by custom converter class.
C#
public MainPage() { InitializeComponent(); var ViewModel = new SelectionViewModel(); listView.BindingContext = ViewModel; listView.ItemsSource = ViewModel.MusicInfo; } private void ListView_OnSelectionChanged(object sender, ItemSelectionChangedEventArgs e) { for (int i = 0; i < e.AddedItems.Count; i++) { var item = e.AddedItems[i]; (item as MusicInfo).IsSelected = true; } for (int i = 0; i < e.RemovedItems.Count; i++) { var item = e.RemovedItems[i]; (item as MusicInfo).IsSelected = false; } }
XAML
<ContentPage> <ContentPage.Resources> <ResourceDictionary> <local:SelectionBoolToBackgroundColorConverter x:Key="BoolToColorConverter"/> </ResourceDictionary> </ContentPage.Resources> <Grid Margin="0"> <Grid.RowDefinitions> <RowDefinition Height="*" /> </Grid.RowDefinitions> <sync:SfListView x:Name="listView" SelectionGesture="Tap" SelectionMode="Single" SelectionChanged="ListView_OnSelectionChanged" ItemSize="70"> <sync:SfListView.ItemTemplate> <DataTemplate x:Name="ItemTemplate" x:Key="ItemTemplate"> <Grid x:Name="grid" BackgroundColor="{Binding Path=IsSelected, Converter={StaticResource BoolToColorConverter}}"> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="1" /> </Grid.RowDefinitions> <Grid RowSpacing="0" ColumnSpacing="0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="40" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Image Source="{Binding SongThumbnail}" HeightRequest="35" WidthRequest="35" VerticalOptions="Center" HorizontalOptions="Center"/> <Grid Grid.Column="1" RowSpacing="1" VerticalOptions="Center"> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Label Text="{Binding SongTitle}" /> <Grid RowSpacing="0" ColumnSpacing="0" Grid.Row="1"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Label Text="{Binding SongAuther}" /> <Label Grid.Column="1" Text="{Binding SongSize}" /> </Grid> </Grid> </Grid> <Frame Grid.Row="1" HasShadow="True" HeightRequest="1"/> </Grid> </DataTemplate> </sync:SfListView.ItemTemplate> </sync:SfListView> </Grid> </ContentPage>
You can change the background color of the view based on the IsSelected property using the SelectionBoolToBackgroundColorConverter class.
public class SelectionBoolToBackgroundColorConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return (bool)value == true ? Color.FromRgb(211,211,211) : Color.White; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
Click here to download the sample.
Conclusion
I hope you enjoyed learning about how to display Selection background in Xamarin.Forms SfListView.
You can refer to our Xamarin.Forms SfListView featuretour page to know about its other groundbreaking feature representations and documentation, to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!