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.