Home > Enterprise >  ListView not updating despite ItemsSource boud to ObservableCollection
ListView not updating despite ItemsSource boud to ObservableCollection

Time:11-22

I am trying to create a ListView to display a bunch of custom cells, and I need it to update when I modify its content. I have bound its ItemsSource to an ObservableCollection, and I have a method that updates this collection whenever I need. This method is called when I add an element to a persistent collection stored with the Settings plugin, or when the page appears because I can also modify that persistent collection from another page. However, the ListView won't update unless I switch to another tab of my app before coming back.

Here's my code:

ViewModel:

    public ObservableCollection<ViewCell> Cells { get; set; }
        public PlugManagerViewModel()
        {
            Title = "Mes prises";
            Cells = new ObservableCollection<ViewCell>();
        }

        public void RefreshPlugList()
        {
            Cells.Clear();
            foreach ((string name, string desc, _) in Settings.PlugListContent)
            {
                CustomCell cell = new CustomCell
                {
                    Title = name,
                    Detail = desc
                };
                Cells.Add(cell);
            }
        }

XAML:

<ListView x:Name="plugList" x:FieldModifier="public" VerticalScrollBarVisibility="Default" SelectionMode="None" RowHeight="90" VerticalOptions="FillAndExpand" ItemSelected="OpenPlugDetail" ItemsSource="{Binding Cells}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <models:CustomCell Title="{Binding Title}" Detail="{Binding Detail}"></models:CustomCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

XAML.cs:

    PlugManagerViewModel _viewModel;

        public PlugManagerPage()
        {
            InitializeComponent();

            _viewModel = (PlugManagerViewModel)this.BindingContext;

        }

        protected override void OnAppearing()
        {
            base.OnAppearing();

            _viewModel.RefreshPlugList();
        }

        public async void AddPlug(object sender, EventArgs e)
        {
            //Code to modify Settings.PlugListContent
            _viewModel.RefreshPlugList();
        }

        public async void OpenPlugDetail(object sender, SelectedItemChangedEventArgs e)
        {
            //Code to change page and reset selected item
        }

I have already looked up a bunch of other threads and I can't find a working solution. Is my binding broken or is it something else ?

CodePudding user response:

The method OnAppearing only be used before the Page becoming visible. So this is the reason why the ListView won't update unless you switch to another tab then switch back.

You can check Page.OnAppearing Method for more information.

CodePudding user response:

In my opinion, the ItemSource should be (in your Example) should be ViewCell.

Some other suggestions:

  1. Use a CollectionView instead of Listview -> CollectionView-Docs
  2. Use another word instead of the misleading "ViewCell"
  3. As the User Guangyu mentioned - if u r not overriding the OnApperaing()-Method, don't use it.
  • Related