Bug 30540 - Binding to a collection of instances of a class with an overloaded indexer
Summary: Binding to a collection of instances of a class with an overloaded indexer
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.4.4
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-05-28 09:31 UTC by Filip Drsek
Modified: 2016-04-09 00:30 UTC (History)
7 users (show)

Tags: AC
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 FIXED

Description Filip Drsek 2015-05-28 09:31:13 UTC
I develop Xamarin.Forms applications on a Windows 7 machine with Visual Studio 2013 and Xamarin 3.11.458.0 (Xamarin version as indicated in Control Panel – Programs – Programs and functions) and run them on a Lenovo Yoga Tablet 2-1050F with Android 4.4.2. I am trying to set up a ListView bound to a collection of objects with an indexer. The ListView is described in XAML as follows:

      <ListView x:Name="lstView" ItemsSource="{Binding ListOfMyListItemIndexed}"><!-- property “ListOfMyListItemIndexed” of the BindingContext -->
        <ListView.ItemTemplate>
          <DataTemplate>
            <ViewCell>
              <Label Text="{Binding [Name]}" />
            </ViewCell>
          </DataTemplate>
        </ListView.ItemTemplate>
      </ListView>

The type of the property “ListOfMyListItemIndexed” of the object used as the BindingContext is System.Collections.ObjectModel.Collection<MyListItemIndexed>, where the class “MyListItemIndexed” is defined as follows:

    public class MyListItemIndexed
    {
        private int id { get; set; }
        private string name { get; set; }

        public object this[string i] // indexer overload 1
        {
            get { return (i == "Id" ? (object)this.id : (i == "Name" ? this.name : null)); }
        }

        public object this[int i] // indexer overload 2
        {
            get { return (i == 0 ? (object)this.id : (i == 1 ? this.name : null)); }
        }

        public MyListItemIndexed(int id, string name)
        {
            this.id = id;
            this.name = name;
        }
    }

The application crashes on the binding (“An exception occurred.”) and the stack trace is:

>	0x12D in System.Reflection.Binder.Default.SelectProperty
 	0x73 in System.MonoType.GetPropertyImpl
 	0x18 in System.Type.GetProperty
 	0x4 in System.Reflection.TypeInfo.GetDeclaredProperty
 	0xD in Xamarin.Forms.ReflectionExtensions.GetProperty
 	0xF3 in Xamarin.Forms.BindingExpression.SetupPart
 	0xA6 in Xamarin.Forms.BindingExpression.ApplyCore
 	0x7E in Xamarin.Forms.BindingExpression.Apply
 	0x5E in Xamarin.Forms.Binding.Apply
 	0x35 in Xamarin.Forms.BindableObject.ApplyBindings
 	0x61 in Xamarin.Forms.BindableObject.SetInheritedBindingContext
 	0x18 in Xamarin.Forms.Element.OnBindingContextChanged
 	0x1 in Xamarin.Forms.Cell.OnBindingContextChanged
 	0x1B in Xamarin.Forms.BindableObject.BindingContextPropertyBindingPropertyChanged
 	0xFA in Xamarin.Forms.BindableObject.SetValueActual
 	0x1FC in Xamarin.Forms.BindableObject.SetValueCore
 	0x75 in Xamarin.Forms.BindableObject.SetValue
 	0x5 in Xamarin.Forms.BindableObject.SetValue
 	0x7 in Xamarin.Forms.BindableObject.set_BindingContext
 	0x39 in Xamarin.Forms.TemplatedItemsList<Xamarin.Forms.ItemsView<Xamarin.Forms.Cell>,Xamarin.Forms.Cell>.CreateContent
 	0x27 in Xamarin.Forms.TemplatedItemsList<Xamarin.Forms.ItemsView<Xamarin.Forms.Cell>,Xamarin.Forms.Cell>.GetOrCreateContent
 	0xE in Xamarin.Forms.TemplatedItemsList<Xamarin.Forms.ItemsView<Xamarin.Forms.Cell>,Xamarin.Forms.Cell>.get_Item
 	0x44 in Xamarin.Forms.Platform.Android.ListViewAdapter.GetCellsFromPosition
 	0x3 in Xamarin.Forms.Platform.Android.ListViewAdapter.GetCellForPosition
 	0x46 in Xamarin.Forms.Platform.Android.ListViewAdapter.GetView
 	0x1E in Android.Widget.BaseAdapter.n_GetView_ILandroid_view_View_Landroid_view_ViewGroup_ at /Users/builder/data/lanes/1780/3518c4ce/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Widget.BaseAdapter.cs:509,4
 	0x23 in object.8e39691a-f690-4232-8fd5-6d7c4519b15b
 	0x52 in Android.Runtime.JNIEnv.CallVoidMethod at /Users/builder/data/lanes/1780/3518c4ce/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:567,4
 	0x8E in Android.Views.ViewGroup.Layout at /Users/builder/data/lanes/1780/3518c4ce/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Views.ViewGroup.cs:3760,5
 	0x64 in Xamarin.Forms.Platform.Android.ViewRenderer<Xamarin.Forms.ListView,Android.Widget.ListView>.OnLayout
 	0x32 in Xamarin.Forms.Platform.Android.ListViewRenderer.OnLayout
 	0x12 in Xamarin.Forms.Platform.Android.FormsViewGroup.n_OnLayout_ZIIII
 	0x2F in object.09e1f900-9872-4c64-992a-777840d255e3
 	0x73 in Android.Runtime.JNIEnv.CallNonvirtualVoidMethod at /Users/builder/data/lanes/1780/3518c4ce/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:1025,4
 	0x21 in Android.Runtime.JNIEnv.CallNonvirtualVoidMethod at /Users/builder/data/lanes/1780/3518c4ce/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:1036,5
 	0x160 in Xamarin.Forms.Platform.Android.FormsViewGroup.MeasureAndLayout
 	0xC6 in Xamarin.Forms.Platform.Android.VisualElementTracker.UpdateLayout
 	0xE in Xamarin.Forms.Platform.Android.VisualElementRenderer<Xamarin.Forms.ListView>.UpdateLayout
 	0x16 in Xamarin.Forms.Platform.Android.ScrollViewContainer.OnLayout
 	0x12 in Android.Views.ViewGroup.n_OnLayout_ZIIII at /Users/builder/data/lanes/1780/3518c4ce/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Views.ViewGroup.cs:4041,4
 	0x2F in object.6cab2db1-cde8-4377-b74e-c6c9ae846aa2
 	0x73 in Android.Runtime.JNIEnv.CallNonvirtualVoidMethod at /Users/builder/data/lanes/1780/3518c4ce/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:1025,4
 	0xE6 in Android.Widget.FrameLayout.OnLayout at /Users/builder/data/lanes/1780/3518c4ce/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Widget.FrameLayout.cs:725,6
 	0x8 in Xamarin.Forms.Platform.Android.ScrollViewRenderer.OnLayout
 	0x12 in Android.Widget.FrameLayout.n_OnLayout_ZIIII at /Users/builder/data/lanes/1780/3518c4ce/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Widget.FrameLayout.cs:703,4
 	0x2F in object.8a91b6c7-962c-4e9d-8437-1eb67292d4f4
 	0x52 in Android.Runtime.JNIEnv.CallVoidMethod at /Users/builder/data/lanes/1780/3518c4ce/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:567,4
 	0x8E in Android.Views.ViewGroup.Layout at /Users/builder/data/lanes/1780/3518c4ce/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Views.ViewGroup.cs:3760,5
 	0x9B in Xamarin.Forms.Platform.Android.VisualElementTracker.UpdateLayout
 	0xE in Xamarin.Forms.Platform.Android.ScrollViewRenderer.UpdateLayout
 	0x38 in Xamarin.Forms.Platform.Android.VisualElementRenderer<Xamarin.Forms.View>.OnLayout
 	0x12 in Xamarin.Forms.Platform.Android.FormsViewGroup.n_OnLayout_ZIIII
 	0x2F in object.09e1f900-9872-4c64-992a-777840d255e3
 	0x73 in Android.Runtime.JNIEnv.CallNonvirtualVoidMethod at /Users/builder/data/lanes/1780/3518c4ce/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:1025,4
 	0x21 in Android.Runtime.JNIEnv.CallNonvirtualVoidMethod at /Users/builder/data/lanes/1780/3518c4ce/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:1036,5
 	0x160 in Xamarin.Forms.Platform.Android.FormsViewGroup.MeasureAndLayout
 	0xC6 in Xamarin.Forms.Platform.Android.VisualElementTracker.UpdateLayout
 	0xE in Xamarin.Forms.Platform.Android.VisualElementRenderer<Xamarin.Forms.View>.UpdateLayout
 	0x38 in Xamarin.Forms.Platform.Android.VisualElementRenderer<Xamarin.Forms.Page>.OnLayout
 	0x12 in Xamarin.Forms.Platform.Android.FormsViewGroup.n_OnLayout_ZIIII
 	0x2F in object.09e1f900-9872-4c64-992a-777840d255e3
 	0x73 in Android.Runtime.JNIEnv.CallNonvirtualVoidMethod at /Users/builder/data/lanes/1780/3518c4ce/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:1025,4
 	0x21 in Android.Runtime.JNIEnv.CallNonvirtualVoidMethod at /Users/builder/data/lanes/1780/3518c4ce/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:1036,5
 	0x160 in Xamarin.Forms.Platform.Android.FormsViewGroup.MeasureAndLayout
 	0xC6 in Xamarin.Forms.Platform.Android.VisualElementTracker.UpdateLayout
 	0xE in Xamarin.Forms.Platform.Android.VisualElementRenderer<Xamarin.Forms.Page>.UpdateLayout
 	0xA0 in Xamarin.Forms.Platform.Android.Platform.OnLayout
 	0x1B in Xamarin.Forms.Platform.Android.PlatformRenderer.OnLayout
 	0x12 in Android.Views.ViewGroup.n_OnLayout_ZIIII at /Users/builder/data/lanes/1780/3518c4ce/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Views.ViewGroup.cs:4041,4
 	0x2F in object.6cab2db1-cde8-4377-b74e-c6c9ae846aa2

Alternatively, if I use another source of binding (where no exception occurs) but I take an instance of the class “MyListItemIndexed” (named for example “x”) and I execute the code

var y = x.GetType().GetTypeInfo().GetDeclaredProperty("Item");

then I get the exception:

System.Reflection.AmbiguousMatchException: Ambiguous matching in method resolution

and the stack trace is:

 	0x12D in System.Reflection.Binder.Default.SelectProperty
 	0x73 in System.MonoType.GetPropertyImpl
 	0x18 in System.Type.GetProperty
 	0x4 in System.Reflection.TypeInfo.GetDeclaredProperty
>	0x11A in Apollo.ServiceGate.MyDataSource..ctor at c:\Users\filip.drsek\Documents\Visual Studio 2013\Projects\TestListOfDictionaries\TestListOfDictionaries\Apollo.ServiceGate\MyDataSource.cs:125,-1
 	0x1 in Apollo.ServiceGate.MyDataSource..cctor at c:\Users\filip.drsek\Documents\Visual Studio 2013\Projects\TestListOfDictionaries\TestListOfDictionaries\Apollo.ServiceGate\MyDataSource.cs:148,-1
 	0x36 in System.Reflection.MonoField.GetValueInternal
 	0x6C in System.Reflection.MonoField.GetValue
 	0xEF in Xamarin.Forms.Xaml.StaticExtension.ProvideValue
 	0x107 in Xamarin.Forms.Xaml.MarkupExtensionParser.Parse
 	0x8A in Xamarin.Forms.Xaml.MarkupExpressionParser.ParseExpression
 	0x76 in Xamarin.Forms.Xaml.ParseMarkupsVisitor.Visit
 	0x3 in Xamarin.Forms.Xaml.MarkupNode.Accept
 	0x2E in Xamarin.Forms.Xaml.RootNode.Accept
 	0xB2 in Xamarin.Forms.Xaml.XamlLoader.Load
 	0x2D in Xamarin.Forms.Xaml.XamlLoader.Load
 	0x7 in Xamarin.Forms.Xaml.Extensions.LoadFromXaml<TestListOfDictionaries.ListPage>
 	0xC in TestListOfDictionaries.ListPage.InitializeComponent at c:\Users\filip.drsek\Documents\Visual Studio 2013\Projects\TestListOfDictionaries\TestListOfDictionaries\TestListOfDictionaries\obj\Debug\ListPage.xaml.g.cs:30,-1
 	0x10 in TestListOfDictionaries.ListPage..ctor at c:\Users\filip.drsek\Documents\Visual Studio 2013\Projects\TestListOfDictionaries\TestListOfDictionaries\TestListOfDictionaries\ListPage.xaml.cs:27,-1
 	0x9 in TestListOfDictionaries.App..ctor at c:\Users\filip.drsek\Documents\Visual Studio 2013\Projects\TestListOfDictionaries\TestListOfDictionaries\TestListOfDictionaries\App.cs:15,-1
 	0x12 in TestListOfDictionaries.Droid.MainActivity.OnCreate at c:\Users\filip.drsek\Documents\Visual Studio 2013\Projects\TestListOfDictionaries\TestListOfDictionaries\TestListOfDictionaries.Droid\MainActivity.cs:20,-1
 	0x13 in Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ at /Users/builder/data/lanes/1780/3518c4ce/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.App.Activity.cs:2707,4
 	0x17 in object.348bac61-8d75-4f76-b10a-ac5a45eec3f4

These exceptions do not occur if the indexer overload with the integer index (overload 2) is removed from the definition of the class “MyListItemIndexed”. This proves that the problem originates from the overloaded indexer. Therefore, it is also impossible to use for example a DataTable as a data source for populating a ListView through binding, because elements in rows of a DataTable can also be accessed by indexing either by column ordinal number, or by column name.
Comment 1 Jason Smith [MSFT] 2016-04-09 00:30:35 UTC
Thank you for taking the time to submit this report. After reviewing the description of this bug, we believe it no longer affects the current version of Xamarin.Forms. If you are still experiencing the issue after updating your packages, please reopen this report with an attached reproduction.
 
For your convenience, we have created some reproduction best practices viewable here: https://gist.github.com/jassmith/92405c300e54a01dcc6d

Warm regards,
Xamarin Forms Team