1
votes

The following Xamarin Forms app that contains a Picker and a CollectionView controls bound to the same List of animals (Chicken and Cow). When I push the upgrade button the animals should upgrade to an Eagle and Elephant. The animals are upgraded, but the Picker doesn't reflect the change. The CollectionView works fine.

What am I doing wrong here? If needed, the source for this project can be found at Github.

Click to see demo

Imgur

MainPage.xaml

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:d="http://xamarin.com/schemas/2014/forms/design"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 mc:Ignorable="d"
                 x:Class="SampleApp.MainPage">

        <StackLayout Margin="0,100,0,0">
            <Button Text="Upgrade Animals" 
                    Margin="10"
                    HorizontalOptions="Center"
                    VerticalOptions="Start"
                    Command="{Binding Upgrade}" />
            <Picker ItemsSource="{Binding Animals}" />
            <CollectionView ItemsSource="{Binding Animals}" />
        </StackLayout>

    </ContentPage>

MainPageModel.cs

    public class MainPageModel : INotifyPropertyChanged
    {
        public MainPageModel()
        {
            _Animals = new List<string>() { "Chicken", "Cow" };
        }

        private List<string> _Animals;
        public List<string> Animals
        {
            get { return _Animals; }
            set
            {
                _Animals = value;

                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Animals"));
            }
        }

        public Command Upgrade
        {
            get
            {
                return new Command(_ =>
                {
                    Animals = new List<string>() { "Eagle", "Elephant" };
                });
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

UPDATE

The above code is running XF 4.3.0.908675. I downgraded to 4.2.0.778463, and the problem goes away. This might be a new bug introduced in the latest XF release.

2
Admittedly a work around, but you could manually clear and re-add the new items to the picker - Patrick Goode
Thanks, I actually tried that already. Same issue. The CollectionView updates fine. The Picker does not. - John Livermore
You mean you removed the items from the picker in code and the items remain? - Patrick Goode
Both Picker and CollectionView are bound to the same list. In the example above I do a new List, but I have tried Animals.Clear() and then adding one by one. Both methods have the same issue. I updated the answer, but this appears to be a XF bug. I downgraded to 4.2.0.778463 and the problem goes away. - John Livermore
out of curiosity, do you have Hot Reload enabled? i have the latest xf and pickers work fine - Ricardo Dias Morais

2 Answers

1
votes

The issue has been confirmed on the reported channel:Issue 8177, and I have tried the pre-release version of Xamarin.Forms(4.4.0.991210-pre2), the binding is working fine now on UWP.

0
votes

I've been dealing with a similar issue lately, and it does appear to be a XF issue.

I fixed the issue in my app by not setting the list to be a "new List()" but rather to create a separate list and then attach it to the bound list.

In your Command, you should try this: List newList = new List() { "Eagle", "Elephant" }; Animals = newList;

This might not work for your example, but this did work for me.