Xamarin Forms Cannot Change image on Listview SelectedItem

I have a label and image inside listview. I have to change the image when an item selected on listview. I am getting the current values of imagename and label text when item selected in viewmodel ,but cannot change the image to a new one. i have called the onproperty changed after changing image.But value not updating.Help please..

        <StackLayout >
                    <ListView x:Name="ser_area_list" HasUnevenRows="True"  ItemsSource="{Binding Servicearealist }" SelectedItem="{Binding dd}" >
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <ViewCell.View>
                                        <StackLayout Orientation="Horizontal" >                                         
                                            <Image Source="{Binding ImageName}" WidthRequest="30" HeightRequest="30"  />
                                            <Label Text="{Binding area}" VerticalTextAlignment="Center" />
                                        </StackLayout>
                                    </ViewCell.View>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>                
                </StackLayout>

viewmodel:

public class PopChkVM : INotifyPropertyChanged
{        
    public event PropertyChangedEventHandler PropertyChanged;
    public List<HSA_value> servicearealist = new List<HSA_value>();

    HSA_value ab = new HSA_value();
    public HSA_value dd
    {
        get
        {
            return ab;
        }
        set
        {
            ab = value;

            if (ab == null)
                return;

            OnPropertyChanged("dd");
            Debug.WriteLine(ab);
            ab.ImageName = "checkedbox.png";
            OnPopertyChanged("ImageName");                                  

        }
    }        

    string imagename;
    public string ImageName
    {
        set
        {
            imagename = value;

            OnPopertyChanged("ImageName");

        }
        get
        {
            return imagename;
        }
    }

    void OnPopertyChanged(string ImageName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(ImageName));
    }         

    public List<HSA_value> Servicearealist
    {
        get { return servicearealist; }
        set
        {
            servicearealist = value;
            OnPropertyChanged("Servicearealist");
        }
    }
    public PopChkVM()
    {
        GetHomeServiceAreas();
    }

    protected virtual void OnPropertyChanged(string servicearealist)
    {
        if (PropertyChanged == null)
            return;

        PropertyChanged(this, new PropertyChangedEventArgs(servicearealist));
    }

    private async void GetHomeServiceAreas()
    {

        RestClient dataService = new RestClient();
        var items = await dataService.GetServiceArea();
        if (items.status.Equals("success"))
        {
            Servicearealist = new List<HSA_value>();
            foreach (HSA_Data data in items.data)
            {
                HSA_value hSA_Value = new HSA_value();
                hSA_Value.area = data.area;
                hSA_Value.ImageName = "unchecked.png";
                Servicearealist.Add(hSA_Value);
            }
        }
        else
        {
            DependencyService.Get<IToast>().LongAlert("No  Data Available!");
        }
    }

}

Model:

public class HSA_value
{         
    public string id { get; set; }
    public string area { get; set; }
    public string ImageName { get; set; }
}

Best Answer

  • VipinKrishnaVipinKrishna ✭✭
    Accepted Answer

    I forgot to implement INotifyPropertyChanged in model class..now its working..

    public class HSA_value : INotifyPropertyChanged
        {         
            public string id { get; set; }
            public string area { get; set; }
    
            string imagename = string.Empty;
    
            public string ImageName
            {
                set
                {
                    imagename = value;
    
                    OnPopertyChanged("ImageName");
    
                }
                get
                {
                    return imagename;
                }
            }
    
            void OnPopertyChanged(string ImageName)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(ImageName));
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
        }
    

Answers

  • CharwakaCharwaka INMember ✭✭✭✭

    Try Changing on Main Thread

  • VipinKrishnaVipinKrishna Member ✭✭
    Accepted Answer

    I forgot to implement INotifyPropertyChanged in model class..now its working..

    public class HSA_value : INotifyPropertyChanged
        {         
            public string id { get; set; }
            public string area { get; set; }
    
            string imagename = string.Empty;
    
            public string ImageName
            {
                set
                {
                    imagename = value;
    
                    OnPopertyChanged("ImageName");
    
                }
                get
                {
                    return imagename;
                }
            }
    
            void OnPopertyChanged(string ImageName)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(ImageName));
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
        }
    
Sign In or Register to comment.