我的 ListView 上有绑定(bind)问题,我收到错误:
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");
});
}
感谢您的宝贵时间!
最佳答案
<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) =>
{});
关于c# - ListView 上下文菜单上的命令绑定(bind)未触发(未找到)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53932467/