Bug 60561 - Binding Doesn't Work on Indexed Collections
Summary: Binding Doesn't Work on Indexed Collections
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-11-07 22:53 UTC by Laura
Modified: 2017-11-13 17:56 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Laura 2017-11-07 22:53:23 UTC
In the sample, if you start removing items from the observable collection by clicking the "Remove Item" button you'll notice the label indicating the first item in the list is not updated.

Is there a workaround?
Comment 1 Laura 2017-11-07 23:06:54 UTC
Here's a link to the sample:
https://www.dropbox.com/s/xniijv2ei4mqlar/BindingContextIndexingSample.7z?dl=0
Comment 2 Paul DiPietro [MSFT] 2017-11-13 17:56:16 UTC
You should likely be using a ViewModel and INotifyPropertyChanged so that the Label is made aware of changes and updates accordingly. Something simple such as this should work:

    public class ViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public ObservableCollection<string> Items
        {
            get { return _items; }
            set { _items = value; }
        }
        ObservableCollection<string> _items;

        public string SelectedItem
        {
            get { return Items[0]; }
        }

        public ViewModel(ObservableCollection<string> items)
        {
            _items = items;
            _items.CollectionChanged += OnItemsCollectionChanged;
        }

        void OnItemsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("SelectedItem"));
        }
    } 

You can then pass in that resource collection to the constructor, or adjust and add the items as desired. You could also simply call PropertyChanged on Items and use the Items[0] binding similar to the original reproduction.