dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.27k stars 1.76k forks source link

CollectionViewHandler2 RelativeSource binding to AncestorType not working #26066

Open rysbilinski opened 4 days ago

rysbilinski commented 4 days ago

Description

Using RelativeSource binding AncestorType doesn't seem to work using CollectionViewHandler2

Steps to Reproduce

  1. Add a collecitonview to a page, and bind a property in the itemtemplate to the PageModel using RelativeBinding source of AncestorType:
<CollectionView.ItemTemplate>
    <DataTemplate>
        <StackLayout Padding="10">
            <Label Text="{Binding .}" />
            <Button
                Command="{Binding ShowDialogCommand, Source={x:RelativeSource AncestorType={x:Type pm:RelativeSourceBindingPageModel}}}"
                CommandParameter="{Binding .}"
                Text="Show Dialog" />
        </StackLayout>
    </DataTemplate>
</CollectionView.ItemTemplate>
  1. Enable CollectionViewHandler2:
#if IOS || MACCATALYST
            builder.ConfigureMauiHandlers(handlers =>
            {
                handlers.AddHandler<Microsoft.Maui.Controls.CollectionView, Microsoft.Maui.Controls.Handlers.Items2.CollectionViewHandler2>();
                handlers.AddHandler<Microsoft.Maui.Controls.CarouselView, Microsoft.Maui.Controls.Handlers.Items2.CarouselViewHandler2>();
            });
#endif
  1. run the app on iOS and you will see that the binding simply doesn't work
  2. disabling CollectionViewHandler2 resolves the issue

See the RelativeSourceBindingPage in the attached reproduction repository CollectionViewHandler2Bugs/Pages/RelativeSourceBindingPage.xaml

Link to public reproduction project repository

https://github.com/rysbilinski/CollectionViewHandler2Bugs

Version with bug

9.0.10 SR1

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

8.0.100 SR10

Affected platforms

iOS

Affected platform versions

No response

Did you find any workaround?

If you use the normal CollectionViewHandler then the binding works as expected

 builder
                .UseMauiApp<App>().ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                })
                .UseMauiCommunityToolkit();
//#if IOS || MACCATALYST
//            builder.ConfigureMauiHandlers(handlers =>
//            {
//                handlers.AddHandler<Microsoft.Maui.Controls.CollectionView, Microsoft.Maui.Controls.Handlers.Items2.CollectionViewHandler2>();
//                handlers.AddHandler<Microsoft.Maui.Controls.CarouselView, Microsoft.Maui.Controls.Handlers.Items2.CarouselViewHandler2>();
//            });
//#endif

Relevant log output

PureWeen commented 3 days ago

possibly related https://github.com/dotnet/maui/issues/25195

kevinxufei commented 1 day ago

I can repro this issue at iOS platform on the latest 17.13.0 Preview 1.0(9.0.100 &9.0.0),but I can't verify it on .net8(8.0.100) due to error CS0234.Image

rysbilinski commented 1 day ago

I can repro this issue at iOS platform on the latest 17.13.0 Preview 1.0(9.0.100 &9.0.0),but I can't verify it on .net8(8.0.100) due to error CS0234.Image

won't this be because this handler was inteoduced in .NET 9.0?