We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to display Selection background on SelectedItem when BackgroundColor is set for view in ItemTemplate?

Platform: Xamarin.Forms |
Control: SfListView

In 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.

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile