Coder Social home page Coder Social logo

syncfusionexamples / selected-image-change-listview-xamarin Goto Github PK

View Code? Open in Web Editor NEW
0.0 5.0 0.0 654 KB

This repository contains sample about how to change selected image in Xamarin.Forms ListView (SfListView)

C# 100.00%
property-change-notification property-changed binding selected-item mvvm bindings itemtemplate xamarin xamarin-forms listview

selected-image-change-listview-xamarin's Introduction

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

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

You can also refer the following article.

https://www.syncfusion.com/kb/11486/how-to-change-selected-image-in-xamarin-forms-listview-sflistview

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

SelectedImageChange

selected-image-change-listview-xamarin's People

Contributors

jayaleshwari avatar sarubala20 avatar vinothkumar-ganesan avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.