Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

ListView binding to ObservableCollection does not update GUI

$
0
0

Hi

Can anyone tell me why this simple code is not working as expected? Does the ListView control not listen for changes in the bound property?

  • Expected: when I click the Add button, the GUI (ListView) updates through the binding and shows the new element in the ObservableCollection.
  • Observed: GUI does not update but always shows the initial three items of the ObservableCollection.
  • Btw: OnAddButtonClicked() does get called and ListViewItems does get updated (checked in the debugger). Calling OnPropertyChanged("ListViewItems") inside OnAddButtonClicked() did not help either.
  • And: if I update the code as follows, the ListView works: add a Label control, bind that label to a 2nd property, update that property inside OnAddButtonClicked(), call OnPropertyChanged() from inside the property setter. So obviously this call to OnPropertyChanged() somehow triggers the ListView to get updated as well...
  • I'm now using Xamarin.Forms version 1.1.0.6201. Didn't work with the previous version either.

XAML:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Test1.MainPage"
             Padding="10" Title="MainPageXaml">
    <StackLayout VerticalOptions="StartAndExpand" HorizontalOptions="Fill">
        <Button Text="Add" Clicked="OnAddButtonClicked" />
        <ListView ItemsSource="{Binding ListViewItems}" />
    </StackLayout>
</ContentPage>

C#:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        ListViewItems = new ObservableCollection<string> { "one", "two", "three" };
        BindingContext = this;
    }

    private void OnAddButtonClicked(object sender, EventArgs e)
    {
        ListViewItems.Add(DateTime.Now.ToString());
        // OnPropertyChanged("ListViewItems");
    }

    public ObservableCollection<string> ListViewItems { get; set; }
}

Markus


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>