|
1 | | -# selected-image-change-listview-xamarin |
2 | | -How to change selected image in Xamarin.Forms ListView (SfListView) |
| 1 | +# How to change selected image in Xamarin.Forms ListView (SfListView) |
| 2 | + |
| 3 | +You can change the selected item image in Xamarin.Forms [SfListView](https://help.syncfusion.com/xamarin/listview/overview?) using [Converters](https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/converters). |
| 4 | + |
| 5 | +You can also refer the following article. |
| 6 | + |
| 7 | +https://www.syncfusion.com/kb/11486/how-to-change-selected-image-in-xamarin-forms-listview-sflistview |
| 8 | + |
| 9 | +**C#** |
| 10 | + |
| 11 | +Defining **IsSelected** property in **Model** with [INotifyPropertyChanged](https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.inotifypropertychanged?view=netcore-3.1). |
| 12 | +``` c# |
| 13 | +namespace ListViewXamarin |
| 14 | +{ |
| 15 | + public class BookInfo : INotifyPropertyChanged |
| 16 | + { |
| 17 | + private bool _isSelected = false; |
| 18 | + |
| 19 | + public bool IsSelected |
| 20 | + { |
| 21 | + get => _isSelected; |
| 22 | + set |
| 23 | + { |
| 24 | + if (_isSelected != value) |
| 25 | + { |
| 26 | + _isSelected = value; |
| 27 | + OnPropertyChanged(); |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + public event PropertyChangedEventHandler PropertyChanged; |
| 32 | + |
| 33 | + private void OnPropertyChanged([CallerMemberName] string name = null) |
| 34 | + { |
| 35 | + if (PropertyChanged != null) |
| 36 | + this.PropertyChanged(this, new PropertyChangedEventArgs(name)); |
| 37 | + |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | +**C#** |
| 43 | + |
| 44 | +Updating the **IsSelected** value in [TapCommand](https://help.syncfusion.com/cr/xamarin/Syncfusion.SfListView.XForms~Syncfusion.ListView.XForms.SfListView~TapCommand.html?) execute method. |
| 45 | +``` c# |
| 46 | +namespace ListViewXamarin |
| 47 | +{ |
| 48 | + public class ViewModel |
| 49 | + { |
| 50 | + private Command<object> _itemTappedCommand; |
| 51 | + |
| 52 | + public Command<object> ItemTappedCommand { get => _itemTappedCommand; set => _itemTappedCommand = value; } |
| 53 | + |
| 54 | + public ViewModel() |
| 55 | + { |
| 56 | + ItemTappedCommand = new Command<object>(ItemTappedExecute); |
| 57 | + } |
| 58 | + |
| 59 | + private void ItemTappedExecute(object obj) |
| 60 | + { |
| 61 | + bool IsSelected = ((obj as Syncfusion.ListView.XForms.ItemTappedEventArgs).ItemData as BookInfo).IsSelected; |
| 62 | + |
| 63 | + if (IsSelected) |
| 64 | + return; |
| 65 | + |
| 66 | + foreach (var item in bookList) |
| 67 | + { |
| 68 | + item.IsSelected = false; |
| 69 | + } |
| 70 | + ((obj as Syncfusion.ListView.XForms.ItemTappedEventArgs).ItemData as BookInfo).IsSelected = true; |
| 71 | + } |
| 72 | + |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | +**C#** |
| 77 | + |
| 78 | +Setting Image based on the **IsSelected** value in **Converter**. |
| 79 | +``` c# |
| 80 | +namespace ListViewXamarin |
| 81 | +{ |
| 82 | + class Converter : IValueConverter |
| 83 | + { |
| 84 | + |
| 85 | + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
| 86 | + { |
| 87 | + return (bool)value ? ImageSource.FromResource("ListViewXamarin.Images.Checked.png") : ImageSource.FromResource("ListViewXamarin.Images.Unchecked.png"); |
| 88 | + } |
| 89 | + |
| 90 | + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) |
| 91 | + { |
| 92 | + throw new NotImplementedException(); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | +**XAML** |
| 98 | + |
| 99 | +Binding **IsSelected** Property and converter to Image Source. |
| 100 | +``` xml |
| 101 | +<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" |
| 102 | + xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" |
| 103 | + xmlns:local="clr-namespace:ListViewXamarin" |
| 104 | + xmlns:syncfusion="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms" |
| 105 | + x:Class="ListViewXamarin.MainPage" Padding="{OnPlatform iOS='0,40,0,0'}"> |
| 106 | + <ContentPage.Resources> |
| 107 | + <ResourceDictionary> |
| 108 | + <local:Converter x:Key="converter"/> |
| 109 | + </ResourceDictionary> |
| 110 | + </ContentPage.Resources> |
| 111 | + |
| 112 | + <ContentPage.Content> |
| 113 | + <syncfusion:SfListView ItemsSource="{Binding bookList}" x:Name="listView" ItemSize="60" SelectionMode="Single" |
| 114 | + AllowKeyboardNavigation="True" TapCommand="{Binding ItemTappedCommand}"> |
| 115 | + <syncfusion:SfListView.ItemTemplate> |
| 116 | + <DataTemplate> |
| 117 | + ... |
| 118 | + <Image Grid.Column="1" x:Name="selectionImage" HeightRequest="30" WidthRequest="30" VerticalOptions="Center" Margin="20,5" |
| 119 | + Source="{Binding IsSelected, Converter={StaticResource converter}}" HorizontalOptions="End" /> |
| 120 | + ... |
| 121 | + </DataTemplate> |
| 122 | + </syncfusion:SfListView.ItemTemplate> |
| 123 | + </syncfusion:SfListView> |
| 124 | + </ContentPage.Content> |
| 125 | + |
| 126 | +</ContentPage> |
| 127 | +``` |
| 128 | +**Output** |
| 129 | + |
| 130 | + |
0 commit comments