Segamoto
Segamoto

Reputation: 315

Observable collection Add object doesn't refresh view

This is my code, when i press button binded with Clicked property addische the new item is added to the ListView but I can't see it in the list.

public partial class GS : ContentPage
{
    private GSViewModel _viewModel;

    public GS()
    {
        InitializeComponent();
        BindingContext = _viewModel = new GSViewModel();
    }

    private void addische(object sender, EventArgs e)
    {
        _viewModel.newItemAdded();
    }

    public class GSViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public ObservableCollection<schItem> _scheListItem;
        public ObservableCollection<schItem> Items { get { return _scheListItem; }
            private set
            {
                if (_scheListItem != value)
                {
                    _scheListItem = value;
                    OnPropertyChanged();
                }
            }
        }

        public void newItemAdded()
        {
            Items.Add(new schItem
            {
                realm_id = 133,
                list_id = 33
            });
        }

        public GSViewModel()
        {
            Items = new ObservableCollection<schItem>();
            initListView();
        }

        public void initListView()
        {
           //get data
        }

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class schItem
    {
        public int realm_id { get; set; }
        public int list_id { get; set; }
        }
    }

Maybe i missing something with INotifyPropertyChanged class.

I'm debugging in Android Device

Upvotes: 0

Views: 128

Answers (1)

Yuri S
Yuri S

Reputation: 5370

Few reasons why you may not see anything.

  1. You bindings are wrong, so you will not see any text. To fix change

          <Label Text = "{Binding name}" HeightRequest="20" FontAttributes="Bold"/>
          <Label Text = "{Binding data, StringFormat='a {0:F0}'}" />
    

to

      <Label Text = "{Binding realm_id}" HeightRequest="20" FontAttributes="Bold"/>
      <Label Text = "{Binding list_id}" />
  1. Check if "delete.png" exists in correct place and it is not "delete.jpg"

  2. After that is fixed remove ListView IsEnabled="False"

enter image description here

Upvotes: 1

Related Questions