MenuFlyoutItem commands not fire

Dani_S 4,481 Reputation points
2024-08-01T07:08:30.9866667+00:00

Hi,

why MenuFlyoutItem command not fire?

<DataTemplate>

 <Label Text="{Binding Name}" VerticalTextAlignment="Center"

ToolTipProperties.Text="{Binding}"  >

     <FlyoutBase.ContextFlyout>

         <MenuFlyout>

             <MenuFlyoutItem Text="New Folder"

                     Command="{Binding NewFolderCommand}"/>

             <MenuFlyoutItem Text="Rename Folder"

                 Command="{Binding RenameFolderCommand}"/>

             <MenuFlyoutItem Text="Delete Folder"

                 Command="{Binding DeleteFolderCommand}"/>

         </MenuFlyout>

     </FlyoutBase.ContextFlyout>

 </Label>

</DataTemplate>

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

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2024-08-02T05:44:03.19+00:00

    Hello,

    I noticed that you used the DataTemplate tag, which means that this item should come from CollectionView. In CollectionView, subitems can only contain items in ItemSource and cannot directly call Commands in ViewModel.

    For example, CollectionView is bound to a list of Models. If the Model class only has Name and Desc properties, subitems could only use these properties as binding items.

    The workaround is to bind the Command to the ViewModel again through relative binding to use the Command. Please refer to the following code and documentation.

    
    <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    
                 xmlns:local="clr-namespace:MauiApp5"
    
                 x:Class="MauiApp5.MainPage">
    
    <ContentPage.BindingContext>
    
    <local:ClockViewModel/>
    
    </ContentPage.BindingContext>
    
    <CollectionView ItemsSource="{Binding Models}">
    
    	<CollectionView.ItemTemplate>
    
    		<DataTemplate>
    
    			<Label Text="{Binding Name}" VerticalTextAlignment="Center" 					ToolTipProperties.Text="{Binding}" >
    
    			<FlyoutBase.ContextFlyout>
    
    				<MenuFlyout>
    
    					<MenuFlyoutItem Text="{Binding Description}"
    
    						Command="{Binding Source={RelativeSource AncestorType={x:Type local:ClockViewModel}}, Path=NewFolderCommand}"/>
    
    				</MenuFlyout>
    
    			</FlyoutBase.ContextFlyout>
    
    			</Label>
    
    		</DataTemplate>
    
    	</CollectionView.ItemTemplate>
    
    </CollectionView>
    
    

    Best Regards,

    Alec Liu.


    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 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

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.