简体   繁体   English

ListView 上下文菜单上的命令绑定未触发(未找到)?

[英]Command binding on ListView context menu not firing (not found)?

I have a binding problem on my ListView I got an error :我的 ListView 有绑定问题,我收到一个错误:

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

Here is the XAML (maybe I made an error with the reference) :这是 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>
...

Here is the ViewModel (ContactsPageContent) :这是 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");
    });
}

Thank you for your time !感谢您的时间 !

 <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>

and in your ViewModel you can use this:在你的 ViewModel 你可以使用这个:

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

Write up your Command in the form of property as below以财产的形式写下您的Command ,如下所示

public ICommand OnEdit { get; set; }
OnEdit= new Command(EditAction); 
private void EditAction(object obj)
{ 
 Debug.Write("OK"); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM