How to change the bacground and text color of MenuFlyoutItem?

Dani_S 3,746 Reputation points
2024-09-25T14:36:05.62+00:00

Hi,

How to change the bacground and text color of MenuFlyoutItem?

<FlyoutBase.ContextFlyout >

 <MenuFlyout  >

     <MenuFlyoutItem Text="Load external configurations" 

         Command="{Binding BindingContext.ExternalConfigurationCommand, Source={x:Reference loadExteranlConfigurationButton}}"/>

         <MenuFlyoutItem Text="Load external logo"

         Command="{Binding BindingContext.ExternalLogoCommand, Source={x:Reference loadExteranlConfigurationButton}}"/>

     </MenuFlyout>

</FlyoutBase.ContextFlyout>

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

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 30,916 Reputation points Microsoft Vendor
    2024-09-26T06:00:16.76+00:00

    Hello,

    There are two ways to modify the text color and the background color.

    The first way: Open Styles.xaml in Recourses/Styles folder, then add the FlyoutItemLabelStyle for the Shell, for example

    <Style TargetType="Label"
        Class="FlyoutItemLabelStyle">
    <Setter Property="TextColor"
             Value="Yellow" />
    <Setter Property="BackgroundColor" Value="Green"> </Setter>
    </Style>
    

    The other way is to define the menuitem appearance, then you can set the color, for example

    <Shell.MenuItemTemplate>
    <DataTemplate>
    <Grid BackgroundColor="Purple" ColumnDefinitions="0.2*,0.8*">
    <Image Source="{Binding Icon}"
                        Margin="5"
                        HeightRequest="45" />
    <Label Grid.Column="1"
                        Text="{Binding Text}"
                        FontAttributes="Italic"
                        TextColor="Red"
                        VerticalTextAlignment="Center" 
                        BackgroundColor="Yellow"/>
    </Grid>
    </DataTemplate>
    </Shell.MenuItemTemplate>
    

    For more details, you can refer to .NET MAUI Shell flyout - .NET MAUI | Microsoft Learn

    Best Regards,

    Wenyan Zhang


    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.