首页 > 解决方案 > ListView 上下文菜单上的命令绑定未触发(未找到)?

问题描述

我的 ListView 出现绑定问题,出现错误:

Binding: 'OnEdit' property not found on 'ContactsViewModel', target property: 'Xamarin.Forms.MenuItem.Command'

这是 XAML(也许我在引用时出错了):

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="Contactium.ContactsPage"
         x:Name="ContactsPageContent">
<ContentPage.ToolbarItems>
...
<TextCell.ContextActions>
      <MenuItem  Command="{Binding Path=BindingContext.OnEdit, Source={x:Reference ContactsPageContent}}" CommandParameter="{Binding .}" Text="EDITER" IsDestructive="True"/>
      <MenuItem Command="{Binding Path=BindingContext.OnDelete, Source={x:Reference ContactsPageContent}}}" CommandParameter="{Binding .}" Text="SUPPRIMER"/>
</TextCell.ContextActions>
...

这是 ViewModel (ContactsPageContent) :

public Command OnEdit(object sender, EventArgs e)
{
    return new Command(() =>
    {
         Debug.Write("OK");
    });
}

public Command OnDelete(object sender, EventArgs e)
{
    return new Command(() =>
    {
         Debug.Write("OK");
    });
}

感谢您的时间 !

标签: c#xamarinmvvmcommand

解决方案


<ListView x:Name="Cities" <ListView.ItemTemplate>
  <DataTemplate>
    <ViewCell>
      <ViewCell.ContextActions>

        <MenuItem Command="{Binding Path=BindingContext.DeleteCommand ,Source={x:Reference Name=Cities}}" CommandParameter="{Binding .}" Text="Delete" IsDestructive="True">

        </MenuItem>

      </ViewCell.ContextActions>

在您的 ViewModel 中,您可以使用它:

DeleteCommand = new Command<Category>(async (selected) =>
            {});

推荐阅读