Xamarin.Forms-绑定到ContextMenu

问题描述 投票:0回答:1

我有一个列表,其中有歌曲作为项目。长按元素会显示一个上下文菜单。

AllSongsViewModel.xaml:

<DataTemplate x:Key="SongTemplate">
    <ViewCell>
        <ViewCell.ContextActions>
            <MenuItem Text="Edit" />
            <MenuItem Text="Delete"/>
        </ViewCell.ContextActions>

        <StackLayout Padding="15,5" VerticalOptions="Center">

            <Label Text="{Binding Title}"
                   FontSize="16"/>
            <Label Text="{Binding Performer}"
                   FontSize="12"/>
        </StackLayout>
    </ViewCell>
</DataTemplate>

这很好用,但是我现在需要绑定,以便根据bool IsAdmin(位于AllSongsViewModel中)打开上下文菜单。>

AllSongsViewModel.cs:
public bool IsAdmin => _authService.LoggedUser.Role == "Admin";

但是我不知道如何将此属性绑定到上下文菜单

我有一个列表,其中有歌曲作为项目。长按该元素应显示一个上下文菜单。 AllSongsViewModel.xaml:[[[[]]]]

c# mvvm xamarin.forms contextmenu datatemplate
1个回答
0
投票
很遗憾,您无法在ViewModel上执行此操作。但是您可以在View Cell上设置BindingContextChange事件,并在其中进行更改,如下所示:

XAML:

<DataTemplate x:Key="SongTemplate"> <ViewCell BindingContextChanged="OnBindingContextChanged"> <StackLayout Padding="15,5" VerticalOptions="Center"> <Label Text="{Binding Title}" FontSize="16"/> <Label Text="{Binding Performer}" FontSize="12"/> </StackLayout> </ViewCell>

© www.soinside.com 2019 - 2024. All rights reserved.