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.19k stars 1.74k forks source link

RC2 - ItemTemplate of CollectionView is not updated at runtime #6535

Closed Sergtek closed 2 years ago

Sergtek commented 2 years ago

Description

The ItemTemplate of the CollectionView does not work when it is changed at runtime, I have configured a VisualState in the CollectionView with a bool and depending on whether it is true or false it should show an ItemTemplate or another but it does not work, I have also tried to use a DataTrigger instead of a VisualState but it doesn't work either.

Repository: https://github.com/nacompllo/MauiCollectionView/tree/test_CollectionViewItemTemplateIssue

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage 
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    BackgroundColor="Black"
    x:Name="TestPage"    
    x:Class="MauiCollectionView.MainPage">
    <ContentPage.Resources>
        <ResourceDictionary>
            <DataTemplate x:Key="DefaultTemplate">
                <StackLayout
                        BackgroundColor="Red">
                    <Label
                            Text="{Binding Name}"
                            TextColor="White"
                            FontAttributes="Bold"
                            FontSize="20"/>
                    <Label
                            Text="Template: DefaultTemplate"
                            TextColor="White"
                            FontSize="20"/>
                </StackLayout>
            </DataTemplate>

            <DataTemplate x:Key="OtherTemplate">
                <StackLayout
                        BackgroundColor="Green">
                    <Label
                            Text="{Binding Name}"
                            TextColor="White"
                            FontAttributes="Bold"
                            FontSize="20"/>
                    <Label
                            Text="Template: OtherTemplate"
                            TextColor="White"
                            FontSize="20"/>
                </StackLayout>
            </DataTemplate>
        </ResourceDictionary>
    </ContentPage.Resources>

    <ScrollView>
        <StackLayout>
            <Button 
                Text="Change ItemTemplate"
                BackgroundColor="NavajoWhite"
                TextColor="Black"
                FontSize="20"
                Command="{Binding ChangeItemTemplateCommand}"/>

            <CollectionView 
                    ItemsSource="{Binding Bots}"
                    HorizontalOptions="FillAndExpand" 
                    VerticalOptions="FillAndExpand">
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup>
                        <VisualState x:Name="defaultTemplate">
                            <VisualState.StateTriggers>
                                <CompareStateTrigger Property="{Binding Source={x:Reference TestPage}, Mode=TwoWay, Path=BindingContext.DefaultItemTemplateEnabled}" Value="True" />
                            </VisualState.StateTriggers>
                            <VisualState.Setters>
                                <Setter Property="ItemTemplate" Value="{StaticResource DefaultTemplate}" />
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState x:Name="otherTemplate">
                            <VisualState.StateTriggers>
                                <CompareStateTrigger Property="{Binding Source={x:Reference TestPage}, Mode=TwoWay, Path=BindingContext.DefaultItemTemplateEnabled}" Value="False" />
                            </VisualState.StateTriggers>
                            <VisualState.Setters>
                                <Setter Property="ItemTemplate" Value="{StaticResource OtherTemplate}" />
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
            </CollectionView>
        </StackLayout>
    </ScrollView>
</ContentPage>

Steps to Reproduce

Clone the attached repository and run the app on Windows or Android and press the "Change ItemTemplate" button which should change the ItemTemplate of the CollectionView but it's not working.

Version with bug

Release Candidate 2

Last version that worked well

Unknown/Other

Affected platforms

Android, Windows, I was not able test on other platforms

Affected platform versions

Android 10, Windows 11

Did you find any workaround?

No response

Relevant log output

No response

Sergtek commented 2 years ago

I am closing the topic since it was my fault, I forgot to inherit from INotifyPropertyChanged in the ViewModel.