Net maui visualstate for IsVisible not working ?

Sami 856 Reputation points
2023-06-04T22:24:23.5333333+00:00

Net maui visualstate for IsVisible not working ? when backgroundcolor is working but when setter property as Isvisible is not working , can you help me thanks ..

<CollectionView.ItemTemplate>
                    <DataTemplate>
                        <Grid BackgroundColor="{DynamicResource BackColor}">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroupList>
                                    <VisualStateGroup x:Name="CommonStates">
                                        <VisualState x:Name="Normal" >
                                            <VisualState.Setters>
                                                <Setter Property="IsVisible" Value="false" TargetName="gridSelected"/>
                                            </VisualState.Setters>
                                        </VisualState>
                                        <VisualState x:Name="Selected">
                                            <VisualState.Setters>
                                                <Setter Property="IsVisible" Value="True" TargetName="gridSelected"/>
                                            </VisualState.Setters>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateGroupList>
                            </VisualStateManager.VisualStateGroups>

                            <Grid.RowDefinitions>
                                <RowDefinition Height="80"/>
                            </Grid.RowDefinitions>
                           
                            <Grid>
                                <Frame HasShadow="False" Padding="0" CornerRadius="3" BackgroundColor="{StaticResource White}" >
                                <Image Source="{Binding .}" Aspect="AspectFit"/>
                                </Frame>
                            
                            </Grid>
                            <Grid x:Name="gridSelected"  HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">

                            </Grid>
                        </Grid>
                    </DataTemplate>
                </CollectionView.ItemTemplate>

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,851 questions
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,261 Reputation points Microsoft Vendor
    2023-06-05T07:21:12.52+00:00

    Hello,

    Please remove <Frame> for <label> and <image> for testing, I found a related known issue about CollectionView does not select an element with a Frame as DataTemplate.

    <CollectionView Grid.Row="2"  x:Name="publishPhotos"  ItemsSource="{Binding Images}" SelectionMode="Single"  HorizontalOptions="FillAndExpand" Margin="5,0" >
            <CollectionView.ItemsLayout>
                <GridItemsLayout Orientation="Vertical" Span="5" HorizontalItemSpacing="5" VerticalItemSpacing="5" />
            </CollectionView.ItemsLayout>
            <CollectionView.ItemTemplate>
                <DataTemplate>
                        
                      <Grid BackgroundColor="{DynamicResource BackColor}" Height="80">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroupList>
                                    <VisualStateGroup x:Name="CommonStates">
                                        <VisualState x:Name="Normal" >
                                            <VisualState.Setters>
                                                <Setter Property="IsVisible" Value="false" TargetName="gridSelected"/>
                                            </VisualState.Setters>
                                        </VisualState>
                                        <VisualState x:Name="Selected">
                                            <VisualState.Setters>
                                                <Setter Property="IsVisible" Value="True" TargetName="gridSelected"/>
                                            </VisualState.Setters>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateGroupList>
                        </VisualStateManager.VisualStateGroups>
                       <Grid.RowDefinitions>
                            <RowDefinition Height="80"/>
                        </Grid.RowDefinitions>
                           <Grid>
                                 <Image Source="{Binding .}" Aspect="AspectFit"/>
                            </Grid>
                            <Grid x:Name="gridSelected"   HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                               
                            </Grid>
                       </Grid>                   
                    </DataTemplate>
            </CollectionView.ItemTemplate>
       </CollectionView>
    

    And change your <VisualState x:Name="isSelected"> to <VisualState x:Name="Selected">

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Sami 856 Reputation points
    2023-06-07T12:23:02.11+00:00

    Finally I found the problem because of references that I have removed them now it is working... Thanks a lot..

    xmlns:android="clr namespace:Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;assembly=Microsoft.Maui.Controls" // removed
    android:Application.WindowSoftInputModeAdjust="Unspecified" // removed
    
    
    <CollectionView.ItemTemplate>
                            <DataTemplate>
                        <Grid BackgroundColor="{DynamicResource BackColor}" IsVisible="{Binding isSelected}">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroupList>
                                    <VisualStateGroup x:Name="CommonStates">
                                        <VisualState x:Name="Normal" >
                                            <VisualState.Setters>
                                                <Setter Property="IsVisible" Value="False" TargetName="gridSelected" />
                                            </VisualState.Setters>
                                        </VisualState>
                                        <VisualState x:Name="Selected">
                                            <VisualState.Setters>
                                                <Setter Property="IsVisible" Value="True" TargetName="gridSelected" />
                                            </VisualState.Setters>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateGroupList>
                            </VisualStateManager.VisualStateGroups>
                            <Grid.RowDefinitions>
                                        <RowDefinition Height="105"/>
                                    </Grid.RowDefinitions>
    
                            <Grid  HeightRequest="80" VerticalOptions="Start">
                                <Border  StrokeShape="RoundRectangle 3" BackgroundColor="{StaticResource BorderColor}"  >
                                            <Image Source="{Binding .}" Aspect="AspectFit"/>
                                        </Border>
    
                                    </Grid>
                            <Grid x:Name="gridSelected"   HorizontalOptions="Center" VerticalOptions="FillAndExpand">
                              
                                        <Border   Stroke="{DynamicResource BorderColor}" BackgroundColor="{StaticResource White}" Padding="1" HeightRequest="20" WidthRequest="20"  Margin="3" HorizontalOptions="End" VerticalOptions="Start">
                                            <Border.StrokeShape>
                                                <RoundRectangle CornerRadius="50" />
                                            </Border.StrokeShape>
                                            <Label  FontSize="{StaticResource XSFontSize}" Style="{StaticResource ionIcon}"  Text="{x:Static icon:IonIcons.Checkmark}" TextColor="{StaticResource City}"  Background="Transparent"  />
                                        </Border>
                                        <Frame HasShadow="False" Padding="6,3" CornerRadius="3" HeightRequest="20" BackgroundColor="{StaticResource City}" VerticalOptions="End" HorizontalOptions="FillAndExpand">
                                            <Label FontSize="{StaticResource XXXSFontSize}" TextColor="{StaticResource White}"  Text="Ana Resim" HorizontalTextAlignment="Center" />
                                        </Frame>
                                    </Grid>
                                </Grid>
                            </DataTemplate>
                        </CollectionView.ItemTemplate>