Bug 60784 - Data binding to object failed to convert
Summary: Data binding to object failed to convert
Status: NEEDINFO
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.5.0
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Stephane Delcroix
URL:
Depends on:
Blocks:
 
Reported: 2017-11-21 02:59 UTC by Hushan
Modified: 2017-11-22 10:50 UTC (History)
2 users (show)

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

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 for Bug 60784 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
NEEDINFO

Description Hushan 2017-11-21 02:59:52 UTC
In the latest XF 2.5.0.91635, data binding to object in itemtemplate does not work anymore, it used to be working.

1. set list view's itemssource to a list of object

    List<DataItem> list = new List<DataItem>();
            for(int i = 0; i < 10; i++)
            {
                list.Add(new DataItem
                {
                    N = "L " + i.ToString(),
                    C = i + 1,
                });
            }
            lv.ItemsSource = list;

2. use items template:

    <ListView x:Name="lv">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Label Text="{Binding}"></Label>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>


3. The list view is expected to show the return string of DataItem's ToString() function, and if it is not overrided, then the default namespace.class should be show, instead, the list view is showing nothing, and log output windows outputs:

[0:] Binding: Models.DataItem can not be converted to type 'System.String'
[0:] Binding: Models.DataItem can not be converted to type 'System.String'
[0:] Binding: Models.DataItem can not be converted to type 'System.String'
[0:] Binding: Models.DataItem can not be converted to type 'System.String'
[0:] Binding: Models.DataItem can not be converted to type 'System.String'


4. If I simply add a ToString override in the DataItem class:

    public override System.String ToString()
        {
            return "AA";
        }

Then the log shows:

[0:] Binding: AA can not be converted to type 'System.String'
[0:] Binding: AA can not be converted to type 'System.String'
[0:] Binding: AA can not be converted to type 'System.String'
[0:] Binding: AA can not be converted to type 'System.String'
[0:] Binding: AA can not be converted to type 'System.String'

Clearly the binding is breaking, it is complaining about cannot convert a string to string! Tried different binding expressions, {Binding}, {Binding Path=.} all the same issue.
Comment 1 Stephane Delcroix 2017-11-21 20:02:09 UTC
Thx for this report. I'll investigate that shortly. Could you please attach a small project triggering this issue ? thanks
Comment 2 Stephane Delcroix 2017-11-22 10:50:04 UTC
I moved back up to 19 months ago (2.3.0), trying to find a release on which this used to work with the following unit test

		[Test]
		public void InvokeToString()
		{
			var property = BindableProperty.Create("foo", typeof(string), typeof(MockBindable), null);
			var bindable = new MockBindable {
				BindingContext = new {
					Foo="Bar"
				}
			};
			bindable.SetBinding(property, ".");

			Assert.That(log.Messages.Count, Is.EqualTo(0),
				"An error was logged: " + log.Messages.FirstOrDefault());
			Assert.That(bindable.GetValue(property), Is.EqualTo("[Binding] { Foo = Bar }"));
		}

no luck.

Could you please indicate which version you were using before (in which it worked) and attach a very small project triggering the issue ?

thanks