Xamarin.Forms icon indicating copy to clipboard operation
Xamarin.Forms copied to clipboard

[Bug] ListView item BindingContext unexpectedly changing

Open MitchBomcanhao opened this issue 4 years ago • 25 comments

Description

This has been observed on UWP, have not been able to test on other platforms.

I've started noticing a lot of debug messages about binding errors on ui elements inside listview items. After debugging a little, it seems that the list items get created twice - I've got 5 items on the list, but 2 sets of 5 items have been built. The datatemplate selector has ran twice. The first set of items have the correct binding context. Then the second set uses the listview's bindingcontext first (which is wrong and causes binding errors) but then corrects itself to the item's bindingcontext.

This second set of items appears to be the one that gets displayed in the UI, judging by their unique ids.

this is the stack when the binding context changed event is fired for the wrong context image then it fires again, with this stack, and this comes with the correct binding context for the list item image

Has anyone seen anything like this, any ideas on what is going on?

Basic Information

  • Version with issue: 4.5.0.657
  • Last known good version: I hadn't seen these errors getting to 4.5, but haven't been able to backtrack and see where it changed
  • IDE: VS 16.5
  • Platform Target Frameworks:
    • iOS: not tested
    • Android: not tested
    • UWP: 17763

MitchBomcanhao avatar Apr 24 '20 11:04 MitchBomcanhao

I can reproduce the issue. Output example:

Binding: 'Title' property not found on 'System.Collections.Generic.List`1[Xamarin.Forms.Controls.CorePageView+GalleryPageFactory]', target property: 'Xamarin.Forms.TextCell.Text'
Binding: 'TitleAutomationId' property not found on 'System.Collections.Generic.List`1[Xamarin.Forms.Controls.CorePageView+GalleryPageFactory]', target property: 'Xamarin.Forms.TextCell.AutomationId'
Binding: 'Title' property not found on 'System.Collections.Generic.List`1[Xamarin.Forms.Controls.CorePageView+GalleryPageFactory]', target property: 'Xamarin.Forms.TextCell.Text'
Binding: 'TitleAutomationId' property not found on 'System.Collections.Generic.List`1[Xamarin.Forms.Controls.CorePageView+GalleryPageFactory]', target property: 'Xamarin.Forms.TextCell.AutomationId'
Binding: 'Title' property not found on 'System.Collections.Generic.List`1[Xamarin.Forms.Controls.CorePageView+GalleryPageFactory]', target property: 'Xamarin.Forms.TextCell.Text'
Binding: 'TitleAutomationId' property not found on 'System.Collections.Generic.List`1[Xamarin.Forms.Controls.CorePageView+GalleryPageFactory]', target property: 'Xamarin.Forms.TextCell.AutomationId'
Binding: 'Title' property not found on 'System.Collections.Generic.List`1[Xamarin.Forms.Controls.CorePageView+GalleryPageFactory]', target property: 'Xamarin.Forms.TextCell.Text'
Binding: 'TitleAutomationId' property not found on 'System.Collections.Generic.List`1[Xamarin.Forms.Controls.CorePageView+GalleryPageFactory]', target property: 'Xamarin.Forms.TextCell.AutomationId'
Binding: 'Title' property not found on 'System.Collections.Generic.List`1[Xamarin.Forms.Controls.CorePageView+GalleryPageFactory]', target property: 'Xamarin.Forms.TextCell.Text'
Binding: 'TitleAutomationId' property not found on 'System.Collections.Generic.List`1[Xamarin.Forms.Controls.CorePageView+GalleryPageFactory]', target property: 'Xamarin.Forms.TextCell.AutomationId'
Binding: 'Title' property not found on 'System.Collections.Generic.List`1[Xamarin.Forms.Controls.CorePageView+GalleryPageFactory]', target property: 'Xamarin.Forms.TextCell.Text'
Binding: 'TitleAutomationId' property not found on 'System.Collections.Generic.List`1[Xamarin.Forms.Controls.CorePageView+GalleryPageFactory]', target property: 'Xamarin.Forms.TextCell.AutomationId'
Binding: 'Title' property not found on 'System.Collections.Generic.List`1[Xamarin.Forms.Controls.CorePageView+GalleryPageFactory]', target property: 'Xamarin.Forms.TextCell.Text'
Binding: 'TitleAutomationId' property not found on 'System.Collections.Generic.List`1[Xamarin.Forms.Controls.CorePageView+GalleryPageFactory]', target property: 'Xamarin.Forms.TextCell.AutomationId'
Binding: 'Title' property not found on 'System.Collections.Generic.List`1[Xamarin.Forms.Controls.CorePageView+GalleryPageFactory]', target property: 'Xamarin.Forms.TextCell.Text'
Binding: 'TitleAutomationId' property not found on 'System.Collections.Generic.List`1[Xamarin.Forms.Controls.CorePageView+GalleryPageFactory]', target property: 'Xamarin.Forms.TextCell.AutomationId'
Binding: 'Title' property not found on 'System.Collections.Generic.List`1[Xamarin.Forms.Controls.CorePageView+GalleryPageFactory]', target property: 'Xamarin.Forms.TextCell.Text'
Binding: 'TitleAutomationId' property not found on 'System.Collections.Generic.List`1[Xamarin.Forms.Controls.CorePageView+GalleryPageFactory]', target property: 'Xamarin.Forms.TextCell.AutomationId'
Binding: 'Title' property not found on 'System.Collections.Generic.List`1[Xamarin.Forms.Controls.CorePageView+GalleryPageFactory]', target property: 'Xamarin.Forms.TextCell.Text'
Binding: 'TitleAutomationId' property not found on 'System.Collections.Generic.List`1[Xamarin.Forms.Controls.CorePageView+GalleryPageFactory]', target property: 'Xamarin.Forms.TextCell.AutomationId'
Binding: 'Title' property not found on 'System.Collections.Generic.List`1[Xamarin.Forms.Controls.CorePageView+GalleryPageFactory]', target property: 'Xamarin.Forms.TextCell.Text'
Binding: 'TitleAutomationId' property not found on 'System.Collections.Generic.List`1[Xamarin.Forms.Controls.CorePageView+GalleryPageFactory]', target property: 'Xamarin.Forms.TextCell.AutomationId'

jsuarezruiz avatar Apr 24 '20 11:04 jsuarezruiz

Any news on this??

On my project, it changes the BindingContext of the Items bound in the ListView to the Page.BindingContext, which, of course, breaks it, and is not the intended behavior.

It happened only on UWP so far.

Tested in Android, WPF and GTK.

tessarolli avatar Jun 18 '20 18:06 tessarolli

@MitchBomcanhao - You'll observe that the binding error happens on the parent control, not the item. There appears to be a bug in the way Xamarin binds where it first attempts to bind to the owner of the list (the ListView), then to the item in the list. I've been programming in WPF and UWP since the first pre-releases. This is a bug. This is not desired behavior and there's no scenario I can think of where this should be the default. I'm having trouble imagining a scenario where you'd want this at all.

The triage people will first try to say "Well, it doesn't impact performance." How will you know if you don't remove the bug and then test it? If you take the time to remove the bug, then just release the fix. Even if I wanted to use the value from the Property on the ListView, you don't use it.

Second, this bug makes it impossible to look for real binding bugs which can play havoc on a large, complex, MVVM application.

DRAirey1 avatar Nov 13 '20 22:11 DRAirey1

What is the status with this bug?

DRAirey1 avatar Nov 16 '20 16:11 DRAirey1

Any update on this?

AwsomeCode avatar Nov 19 '20 12:11 AwsomeCode

I always ignored the Binding-Errors in the output window, cause of this Bug. But now, you get also a Message in your UWP-App (Debug Bar at the Top) about Binding Errors. image

This bug also invalidates the use case of the "XAML Binding Failures" Window

lewsimon avatar Nov 30 '20 12:11 lewsimon

BUMP ListView work, but Debug show XAML Binding Failures suka bleat'

yellow444 avatar Jan 07 '21 15:01 yellow444

I thought I'd try creating a DataTemplateSelector with which I return a blank template (no bindings to fail) for an object of any inconsistent type. It was a right pain to create and didn't fix the issue because the objects passed through to the DataTemplateSelector was of the correct type but somewhere between selecting the template and binding it swapped the item for the ListView BindingContext anyway. So the next thing I tries was to create a dummy BindingContext for the ListView and then use a RelativeSource back up to the ContentPage for the ItemsSource. <ListView ItemsSource="{Binding Source={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContentPage}}, Path=BindingContext.MyViewModel.MyListItems}" <ListView.BindingContext> <vm:MyListItem BoundProperty="dummy" /> </ListView.BindingContext> </ListView> This did work and resolved the XAML Binding Errors. Basically this is a real pain. Sort it out!

TomRobson-me avatar Feb 05 '21 03:02 TomRobson-me

Is there any update on this bug? It still happens on Xamarin UWP. Tried to use x:DataType, but it doesn't work

<ListView BackgroundColor="Transparent"
                      ItemsSource="{Binding JobList}"
                      SelectedItem="{Binding SelectedItem}"
                          x:Name="lstItems">
                <ListView.ItemTemplate>
                    <DataTemplate x:DataType="model:JobModel" >
                        <ViewCell>

                            <StackLayout>

                                <Grid HorizontalOptions="FillAndExpand" Margin="0,5" Padding="5" ColumnDefinitions="*,2*" RowDefinitions="Auto">
                                    <Label Text="{Binding JobNumber}" Grid.Column="0" VerticalOptions="Start"/>
                                    <Label Text="{Binding Job}" Grid.Column="1" VerticalOptions="Start"/>
                                </Grid>
                            </StackLayout>
                        </ViewCell>
                       
                    </DataTemplate>
                </ListView.ItemTemplate>
                
            </ListView>

gnguyen7 avatar Feb 17 '21 01:02 gnguyen7

I can confirm bug on Xamarin UWP framework version 5.0.0.2012 . The bug doesn't occur on Android and iOS version.

maxiking01 avatar Mar 09 '21 13:03 maxiking01

Does anyone know if this issue occurs on Xamarin.Forms 4.5.0.530 or if this is an issue with all of 4.5.x versions? (maxiking01 said that the current latest version, 5.0.0.2012, still has the issue.) I'd like to update to at least that version in order to add custom fonts without going having to do it separately for each platform.

rpetersonfujitsu avatar Apr 07 '21 16:04 rpetersonfujitsu

Does anyone know if this issue occurs on Xamarin.Forms 4.5.0.530 or if this is an issue with all of 4.5.x versions? (maxiking01 said that the current latest version, 5.0.0.2012, still has the issue.) I'd like to update to at least that version in order to add custom fonts without going having to do it separately for each platform.

unfortunately, 4.5.x versions and UWP appear to be incompatible.

I can confirm the current latest version, 5.0.0.2012, still has the issue.

I have found a work around though.

If you add the model properties to the viewmodel, the binding errors will disappear. of course, the properties will be entirely useless and should be completely unnecessary.

I'm taking a deeper dive into learning how bindings work. I have much to learn but I believe a good starting point to finding the root of this issue is here: https://github.com/xamarin/Xamarin.Forms/blob/caab66bcf9614aca0c0805d560a34e176d196e17/Xamarin.Forms.Core/BindingExpression.cs#L134

confessore avatar May 02 '21 12:05 confessore

This issue still seems to be open for Xamarin.Forms 5.0.0.2083 and I don't see any mention of it in the release notes.

It seems like this issue about list views not being recycled could be related (just a wild guess).

Can anyone confirm if this is still an issue in Xamarin.Forms 5.0.0.2083? (the latest as of time of writing)

rpetersonfujitsu avatar Jul 21 '21 16:07 rpetersonfujitsu

Does anyone have an update on this? I have been fighting with a ListView for the last hour only to find out this is the issue. Not great.

@rpetersonfujitsu I am seeing the same issue on Xamarin.Forms 5.0.0.2083.

RuddyOne avatar Aug 06 '21 14:08 RuddyOne

@Ruddy2007 i had this issue and solved it by moving from using XAML to using the new C# Fluent Markup with XamarinCommunityToolkit in preparation for MAUI

big architecture change but totally worth it

https://docs.microsoft.com/en-us/xamarin/community-toolkit/markup https://github.com/xamarin/XamarinCommunityToolkit/blob/main/samples/XCT.Sample/Pages/Markup/SearchPage.cs https://github.com/xamarin/XamarinCommunityToolkit/blob/main/samples/XCT.Sample/Pages/Markup/SearchPage.logic.cs

confessore avatar Aug 06 '21 17:08 confessore

@confessore I cant switch over at this point I'm afraid.

I can see the binding context changes from the object to the VM and back to the object... this is breaking the binding and nothing is updating after this. Any way to stop the binding from changed after the first one? I have been at this a while and cannot find a single workaround.

RuddyOne avatar Aug 10 '21 08:08 RuddyOne

Hello still have the "Binding property not found" issue with Xamarin.Forms 5.0.0.20.83, only on UWP. It works well on Android and iOS.

NilkOne avatar Sep 17 '21 10:09 NilkOne

I am having similar issue on .Net Maui on Windows platform. Any update on this?

IndexOutOfLimit avatar Mar 29 '22 18:03 IndexOutOfLimit

Yeah, just wasted a few hours of my time chasing shadows because of this. Probably not even the first time.

AceCoderLaura avatar Jun 03 '22 02:06 AceCoderLaura

Yeah, just wasted a few hours of my time chasing shadows because of this. Probably not even the first time.

since maui is built on top of xamarin and the original xamarin team has departed microsoft, i would not expect this bug to be fixed internally by microsoft for many years.

unsubscribing from this list

confessore avatar Jun 03 '22 13:06 confessore

Still seeing these spurious binding errors in MAUI .NET 6

jfoshee avatar Aug 22 '22 18:08 jfoshee