Search code examples
xamarin.formsxamarin.forms-stylesxamarin.forms.shell

Xamarin Forms Shell FlyoutItem Disabled VisualState not working


I'm unable to change the style of the disabled FlyoutItem (IsEnabled=False) using the VisualStates in XAML. It is correctly disabled as I'm unable to click on it.

<?xml version="1.0" encoding="UTF-8"?>
<Shell xmlns="http://xamarin.com/schemas/2014/forms"
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
       xmlns:views="clr-namespace:MyApp.Views"
       xmlns:volDash="clr-namespace:MyApp.Views.VolDash"
       xmlns:orgDash="clr-namespace:MyApp.Views.OrgDash"
       x:Class="MyApp.AppShell"
       FlyoutBackgroundColor="#337ab7">
    <Shell.Resources>
        <Style x:Key="FlyoutItemStyle" TargetType="Grid">
            <Setter Property="VisualStateManager.VisualStateGroups">
                <VisualStateGroupList>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal" />
                        <VisualState x:Name="Selected">
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor" Value="Red"/>
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState x:Name="Disabled">
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor" Value="Green"/>
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateGroupList>
            </Setter>
        </Style>
    </Shell.Resources>
    <Shell.ItemTemplate>
        <DataTemplate>
            <Grid HeightRequest="{x:OnPlatform Android=50}" Style="{StaticResource FlyoutItemStyle}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="{x:OnPlatform Android=54, iOS=50}"></ColumnDefinition>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Image Source="{Binding FlyoutIcon}"
                    VerticalOptions="Center"
                    HorizontalOptions="Center"
                    HeightRequest="{x:OnPlatform Android=24, iOS=22}"
                    WidthRequest="{x:OnPlatform Android=24, iOS=22}">
                </Image>
                <Label VerticalOptions="Center"
                        Text="{Binding Title}"
                        FontSize="{x:OnPlatform Android=14, iOS=Small}"
                        FontAttributes="Bold" Grid.Column="1">
                    <Label.TextColor>
                        <OnPlatform x:TypeArguments="Color">
                            <OnPlatform.Platforms>
                                <On Platform="Android" Value="#D2000000" />
                            </OnPlatform.Platforms>
                        </OnPlatform>
                    </Label.TextColor>
                    <Label.Margin>
                        <OnPlatform x:TypeArguments="Thickness">
                            <OnPlatform.Platforms>
                                <On Platform="Android" Value="20, 0, 0, 0" />
                            </OnPlatform.Platforms>
                        </OnPlatform>
                    </Label.Margin>
                    <Label.FontFamily>
                        <OnPlatform x:TypeArguments="x:String">
                            <OnPlatform.Platforms>
                                <On Platform="Android" Value="sans-serif-medium" />
                            </OnPlatform.Platforms>
                        </OnPlatform>
                    </Label.FontFamily>
                </Label>
            </Grid>
        </DataTemplate>
    </Shell.ItemTemplate>
    <FlyoutItem x:Name="volFlyoutItem" Title="Volunteer Dashboard" FlyoutDisplayOptions="AsSingleItem" FlyoutIcon="ic_dashboard_white.png">
        <ShellContent Title="Signups" Icon="ic_assignment.png">
            <volDash:SignupsPage />
        </ShellContent>
        <ShellContent Title="Events" Icon="ic_event.png">
            <volDash:AreasPage />
        </ShellContent>
        <ShellContent Title="Mailbox" Icon="ic_mail_outline.png">
            <volDash:MailboxPage />
        </ShellContent>
        <ShellContent Title="Rankings" Icon="fa_trophy.png">
            <volDash:MyRankingsPage />
        </ShellContent>
        <ShellContent Title="Videos" Icon="ic_ondemand_video.png">
            <volDash:TrainingVideoCategoriesPage />
        </ShellContent>
    </FlyoutItem>
    <FlyoutItem x:Name="orgFlyoutItem" Title="Organizer Dashboard" FlyoutDisplayOptions="AsSingleItem" FlyoutIcon="ic_dashboard_white.png" IsEnabled="False">
        <ShellContent Title="Events" Icon="ic_event.png">
            <orgDash:EventsPage />
        </ShellContent>
    </FlyoutItem>
    <ShellContent Title="Account" Icon="ic_account_box_white.png">
        <views:AccountPage />
    </ShellContent>
    <MenuItem Text="Logout"
            Icon="ic_dashboard_white.png"
            Command="{Binding LogOutCommand}" />
</Shell>

enter image description here


Solution

  • Do you want to achieve result like following GIF?

    enter image description here

    If so, you can change the style like following code.

     <Style x:Key="FlyoutItemStyle" TargetType="Grid">
                <Setter Property="VisualStateManager.VisualStateGroups">
                <VisualStateGroupList>
                    <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal" >
                                <VisualState.Setters>
                                    <Setter Property="BackgroundColor" Value="Green"/>
                                </VisualState.Setters>
                            </VisualState>
                            <VisualState x:Name="Selected">
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor" Value="Red"/>
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateGroupList>
                </Setter>
            </Style>
    

    Update

    I test FlyoutItemStyle style. Disabled properites is not worked for Gird. If you set the IsEnabled="False" for Gird.

    The "Organizer Dashboard" FlyoutItem has the IsEnabled="False" flag

    If you want to change the color to green for FlyoutItem when IsEnabled="False", but your TargetType="Grid" in FlyoutItemStyle. TargetType is not FlyoutItem directly. However, if you set the TargetType to FlyoutItem, FlyoutItem do not have BackgroundColor Property.

    In this case the user cannot tap on it. I need to use that for now, because >there is no IsVisible flag.

    FlyoutItem do not have IsVisible, but Grid have this property. You can add IsVisible="{Binding IsEnabled}" in the Grid tab.

    <Grid HeightRequest="{x:OnPlatform Android=50}" Style="{StaticResource FlyoutItemStyle}"  IsVisible="{Binding IsEnabled}">
    

    Here is running sceenshot.

    enter image description here