Articles in this section
Category / Section

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

2 mins read

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.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied