Bug 53987 - Strange message about wrong DataBinding of ObservableCollection to ItemsSource of ListView when item removed
Summary: Strange message about wrong DataBinding of ObservableCollection to ItemsSourc...
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: 2.3.3
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jimmy [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2017-03-26 11:48 UTC by marek.prerovsky
Modified: 2017-03-30 16:55 UTC (History)
2 users (show)

Tags: android, databinding, viewcell
Is this bug a regression?: ---
Last known good build:


Attachments
repro project for VS 2015 (299.36 KB, application/x-zip-compressed)
2017-03-26 11:48 UTC, marek.prerovsky
Details


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 FIXED

Description marek.prerovsky 2017-03-26 11:48:47 UTC
Created attachment 20849 [details]
repro project for VS 2015

Original discussion: https://forums.xamarin.com/discussion/91311/is-this-a-bug-in-binding-to-itemssource-of-listview

Hi, 
I have prepared minimal project to reproduce the issue. 

The problem is when removing item from ObservableCollection<> binded to ItemsSource of ListView then the BindingContext of ViewCell is set for a while to the object being removed.

Run the project in Android emulator and when you click 'Remove' button then you can see in debug output this message about strange binding error:
[0:] Binding: 'Part' property not found on 'XamBugListView01.MainPage+ItemPart', target property: 'Xamarin.Forms.Label.BindingContext'

The project uses Xamarin.Forms 2.3.3.193. 

Here is the source code of MainPage.xml.cs:

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Xamarin.Forms;
    
    namespace XamBugListView01
    {
        public partial class MainPage : ContentPage
        {
            private ObservableCollection<Item> _allItems = new ObservableCollection<Item>();
    
            public MainPage()
            {
                _allItems.Add(new Item());
                _allItems.Add(new Item());
    
                InitializeComponent();
    
                _listView.BindingContext =
                    new
                    {
                        AllItems = _allItems
                    };
            }
    
            private void RemoveClicked(object sender, EventArgs ea)
            {
                _allItems.RemoveAt(0);
            }
    
            public class Item
            {
                public ItemPart Part { get; private set; }
    
                public Item()
                {
                    Part = new ItemPart();
                }
            }
    
            public class ItemPart
            {
                private static int _counter;
    
                public string Name { get; private set; }
    
                public ItemPart()
                {
                    Name = "item part " + (++_counter);
                }
    
                //public ItemPart Part
                //{
                //    get
                //    {
                //        throw new InvalidOperationException("Property Part of ItemPart is never used in any code and any XAML.");
                //    }
                //}
            }
        }
    }
    
Here is the source code of MainPage.xml:

    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:local="clr-namespace:XamBugListView01"
                 x:Class="XamBugListView01.MainPage">
    
      <StackLayout Orientation="Vertical">
        
        <ListView Grid.Row="1"
                  x:Name="_listView"
                  ItemsSource="{Binding Path=AllItems}"
                  HorizontalOptions="FillAndExpand"
                  VerticalOptions="FillAndExpand">
          <ListView.ItemTemplate>
            <DataTemplate>
              <ViewCell>
                <Label BindingContext="{Binding Path=Part}" Text="{Binding Path=Name}" />
              </ViewCell>
            </DataTemplate>
          </ListView.ItemTemplate>
        </ListView>
    
        <Button Text="Remove"
                Clicked="RemoveClicked" />
        
      </StackLayout>
    
    </ContentPage>
    
Everything else is generated from the template 'Blank Xaml App (Xamarin.Forms.Portable) C#' (project name is XamBugListView01).

Whole solution is attached in ZIP archive.
Comment 1 Jimmy [MSFT] 2017-03-27 14:41:28 UTC
Thank you for filing this report! Let me run the attached project to see if I can confirm this issue and I will update the report with my results.
Comment 2 Jimmy [MSFT] 2017-03-30 16:55:46 UTC
I was able to reproduce this with the attached project on Android and with Forms 2.3.3.193. When the item was removed the debug output would show:

> [0:] Binding: 'Part' property not found on 'XamBugListView01.MainPage+ItemPart', target property: 'Xamarin.Forms.Label.BindingContext'

However I also tested with Forms 2.3.4-pre6 and the issue appears to be resolved. The message is no longer displayed in the output after removing the item.


## Version Tests
2.3.4-pre6  GOOD
2.3.3.193   BAD