Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Bug] ListView item BindingContext unexpectedly changing #10437

Open
MitchBomcanhao opened this issue Apr 24, 2020 · 25 comments
Open

[Bug] ListView item BindingContext unexpectedly changing #10437

MitchBomcanhao opened this issue Apr 24, 2020 · 25 comments

Comments

@MitchBomcanhao
Copy link

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 MitchBomcanhao added s/unverified New report that has yet to be verified t/bug 🐛 labels Apr 24, 2020
@pauldipietro pauldipietro added this to New in Triage Apr 24, 2020
@jsuarezruiz jsuarezruiz added a/binding ⛓ a/listview Problems with the ListView/TableView p/UWP labels Apr 24, 2020
@jsuarezruiz jsuarezruiz moved this from New to Ready For Work in Triage Apr 24, 2020
@jsuarezruiz jsuarezruiz added e/3 🕒 3 and removed s/unverified New report that has yet to be verified labels Apr 24, 2020
@jsuarezruiz
Copy link
Contributor

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'

@samhouts samhouts added this to To do in UWP Ready For Work May 1, 2020
@samhouts samhouts removed this from Ready For Work in Triage May 1, 2020
@tessarolli
Copy link
Contributor

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.

@samhouts samhouts added this to the 5.0.0 milestone Aug 13, 2020
@samhouts samhouts added this to To do in vNext+1 (5.0.0) Aug 13, 2020
@samhouts samhouts removed this from the 5.0.0 milestone Nov 2, 2020
@DRAirey1
Copy link

DRAirey1 commented Nov 13, 2020

@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
Copy link

What is the status with this bug?

@AwsomeCode
Copy link

Any update on this?

@lewsimon
Copy link

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

@yellow444
Copy link

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

@TomRobson-me
Copy link

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!

@gnguyen7
Copy link

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>

@maxiking01
Copy link

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

@rpetersonfujitsu
Copy link

rpetersonfujitsu commented Apr 7, 2021

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.

@confessore
Copy link

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:

BindingDiagnostics.SendBindingFailure(Binding, current, target, property, "Binding", PropertyNotFoundErrorMessage, part.Content, current, target.GetType(), property.PropertyName);

@rpetersonfujitsu
Copy link

rpetersonfujitsu commented Jul 21, 2021

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)

@RuddyOne
Copy link

RuddyOne commented Aug 6, 2021

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.

@confessore
Copy link

@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

@RuddyOne
Copy link

@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.

@NilkOne
Copy link

NilkOne commented Sep 17, 2021

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.

@IndexOutOfLimit
Copy link

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

@AceCoderLaura
Copy link

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

@confessore
Copy link

confessore commented Jun 3, 2022

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

@jfoshee
Copy link

jfoshee commented Aug 22, 2022

Still seeing these spurious binding errors in MAUI .NET 6

@KnownByBJs
Copy link

Bump.
Quality matters

@bsloehr
Copy link

bsloehr commented Mar 21, 2023

Bump, just wasted a couple hours tracking this down in MAUI/.Net7. Please - just fix this. Why does the Windows member of the Android/iOS/Windows triad get the least love?

@RedGlow
Copy link

RedGlow commented Sep 18, 2023

Finally got here after a long search. The bare minimum would be a note in the documentation at least.

@darwin-c-sjh
Copy link

darwin-c-sjh commented Apr 26, 2024

This problem also breaks MAUI RelayCommands when binding a List Item to a CommandParameter using
CommandParameter="{Binding .}"
when the Command has a typed parameter (corresponding to the list item Type), e.g.

[RelayCommand]
private Task Reset(MyListItem item)
{
   // Do something...
}

This results in a runtime exception becasue {Binding .} initially returns the List, then the binding context changes to the List Item

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
Development

No branches or pull requests