xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

[Bug] ListView item BindingContext unexpectedly changing #10437

Open MitchBomcanhao opened 4 years ago

MitchBomcanhao commented 4 years ago

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

jsuarezruiz commented 4 years ago

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'
tessarolli commented 4 years ago

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.

DRAirey1 commented 3 years ago

@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 commented 3 years ago

What is the status with this bug?

AwsomeCode commented 3 years ago

Any update on this?

lewsimon commented 3 years ago

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 commented 3 years ago

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

TomRobson-me commented 3 years ago

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}"

` This did work and resolved the XAML Binding Errors. Basically this is a real pain. Sort it out!
gnguyen7 commented 3 years ago

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 commented 3 years ago

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

rpetersonfujitsu commented 3 years ago

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 commented 3 years ago

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

rpetersonfujitsu commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

@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 commented 3 years ago

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 commented 2 years ago

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

AceCoderLaura commented 2 years ago

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

confessore commented 2 years ago

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 commented 2 years ago

Still seeing these spurious binding errors in MAUI .NET 6

KnownByBJs commented 1 year ago

Bump. Quality matters

bsloehr commented 1 year ago

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 commented 1 year ago

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

darwin-c-sjh commented 6 months ago

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