Search code examples
listviewmvvmxamarin.formssyncfusion

For binded listview how to bind icon of row for selectefd row


I have binded list with iteam,and i want to bind icon of success for selected item.How to do that,to i set for selected item in list view see icon of success for that row. enter image description here

My xaml code:

ViewCell
    ViewCell.View
        Grid
            Grid.RowDefinitions  
                RowDefinition Height="*"Â
                RowDefinition Height="1"Â
            Grid.RowDefinitions  
           Grid
                Grid.ColumnDefinitions
                    ColumnDefinition Width="*"Â
                    ColumnDefinition Width="Auto"Â
                Grid.ColumnDefinitions
                Label Grid.Row="0" Grid.Column="0" LineBreakMode="NoWrap" Text="{Binding NazivRegije}" VerticalOptions="Center" HorizontalOptions="Start" FontAttributes="Bold" TextColor="Teal" FontSize="Medium"
            Grid
            StackLayout Grid.Row="1" Grid.ColumnSpan="2" BackgroundColor="Gray" HeightRequest="1"

            Image Grid.Column="1" x:Name="selectionImage" IsVisible="True" Source="SelectedGreen.png" HorizontalOptions="End" VerticalOptions="Center"
        Grid
    ViewCell.View
/ViewCell

Solution

  • Since you had used MVVM , you just need to bind the property IsVisible of image to a property of your model .

    in xaml

    <ListView  ItemsSource="{Binding MyItemsSource}" SelectedItem="{Binding SelectItems}">
    
    <Image Grid.Column="1" x:Name="selectionImage" IsVisible="{Binding IsVisible}" Source="SelectedGreen.png" HorizontalOptions="End" VerticalOptions="Center"/>
    

    in code behind

    The icon will visible when you click the item .

    in model

    public class MyModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
    
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    
        public string NazivRegije { get; set; }
    
        private bool isVisible;
        public bool IsVisible
        {
            set
            {
                if (isVisible != value)
                {
                    isVisible = value;
                    NotifyPropertyChanged("IsVisible");
                }
            }
            get { return isVisible; }
        }
    
        public MyModel()
        {
            IsVisible = false; // set it as false in default
        }
    }
    

    in viewmodel

    public class MyViewModel : INotifyPropertyChanged
    {
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public ObservableCollection<MyModel> MyItemsSource { get; set; }
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    
        MyModel selectItem;
        public MyModel SelectItems
        {
            get
            {
                return selectItem;
            }
    
            set
            {
                if (selectItem != value)
                {
                    selectItem = value;
                    NotifyPropertyChanged("IsVisible");
    
                    //check or uncheck the item here
    
                    SelectItems.IsVisible = !SelectItems.IsVisible;
    
                }
            }
        }
    
        public MyViewModel()
        {
            MyItemsSource = new ObservableCollection<MyModel>() {
    
                  new MyModel(){NazivRegije="11111" },
                  new MyModel(){NazivRegije="11111" },
                  new MyModel(){NazivRegije="11111" },
                  new MyModel(){NazivRegije="11111" },
                  new MyModel(){NazivRegije="11111" },
                  new MyModel(){NazivRegije="11111" },
                  new MyModel(){NazivRegije="11111" },
                  new MyModel(){NazivRegije="11111" },
            };
        }
    
    }