Articles in this section
Category / Section

How to change selected image in Xamarin.Forms ListView (SfListView)?

2 mins read

You can change the selected item image in Xamarin.Forms SfListView using Converters.

C#

Defining IsSelected property in Model with INotifyPropertyChanged.

namespace ListViewXamarin
{
    public class BookInfo : INotifyPropertyChanged
    {
        private bool _isSelected = false;
 
        public bool IsSelected
        {
            get => _isSelected;
            set
            {
                if (_isSelected != value)
                {
                    _isSelected = value;
                    OnPropertyChanged();
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
 
        private void OnPropertyChanged([CallerMemberName] string name = null)
        {
            if (PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
 
        }
    }
}

C# 

Updating the IsSelected value in TapCommand execute method.

namespace ListViewXamarin
{
    public class ViewModel
    {
        private Command<object> _itemTappedCommand;
 
        public Command<object> ItemTappedCommand { get => _itemTappedCommand; set => _itemTappedCommand = value; }
 
        public ViewModel()
        {
            ItemTappedCommand = new Command<object>(ItemTappedExecute);
        }
 
        private void ItemTappedExecute(object obj)
        {
            bool IsSelected = ((obj as Syncfusion.ListView.XForms.ItemTappedEventArgs).ItemData as BookInfo).IsSelected;
 
            if (IsSelected)
                return;
 
            foreach (var item in bookList)
            {
                item.IsSelected = false;
            }
            ((obj as Syncfusion.ListView.XForms.ItemTappedEventArgs).ItemData as BookInfo).IsSelected = true;
        }
 
    }
}

C#

Setting Image based on the IsSelected value in Converter.

namespace ListViewXamarin
{
    class Converter : IValueConverter
    {
 
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool)value ? ImageSource.FromResource("ListViewXamarin.Images.Checked.png") : ImageSource.FromResource("ListViewXamarin.Images.Unchecked.png");
        }
 
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

XAML

Binding IsSelected Property and converter to Image Source.

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:ListViewXamarin"
             xmlns:syncfusion="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms"
             x:Class="ListViewXamarin.MainPage" Padding="{OnPlatform iOS='0,40,0,0'}">
    <ContentPage.Resources>
        <ResourceDictionary>
            <local:Converter x:Key="converter"/>
        </ResourceDictionary>
    </ContentPage.Resources>
 
    <ContentPage.Content>
        <syncfusion:SfListView ItemsSource="{Binding bookList}" x:Name="listView" ItemSize="60" SelectionMode="Single"
                               AllowKeyboardNavigation="True" TapCommand="{Binding ItemTappedCommand}">
            <syncfusion:SfListView.ItemTemplate>
                <DataTemplate>
                    ...
                    <Image Grid.Column="1" x:Name="selectionImage" HeightRequest="30" WidthRequest="30" VerticalOptions="Center" Margin="20,5"
                               Source="{Binding IsSelected, Converter={StaticResource converter}}" HorizontalOptions="End" />
                    ...
                </DataTemplate>
            </syncfusion:SfListView.ItemTemplate>
        </syncfusion:SfListView>
    </ContentPage.Content>
 
</ContentPage>

Output

Change selected image in SfListView

View sample in GitHub 


Conclusion

I hope you enjoyed learning about how to change selected image in Xamarin.Forms ListView (SfListView).

You can refer to our Xamarin.Forms ListView feature tour page to know about its other groundbreaking feature representations. You can also explore our Xamarin.Forms ListView 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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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