c# - 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");
});
}
感谢您的时间 !
解决方案
<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) =>
{});
推荐阅读
- dataframe - 如何在pyspark中按有序分类变量创建和排序
- shell - 如何为 gsub 使用动态字段值?
- cmake - 使用 wxWidgets 的项目的 cmake 链接器配置
- javascript - 如何获取 Evernote 编辑器的当前元素光标所在的位置?
- javascript - 从数组中动态添加查询参数并在 Postman 请求中迭代
- sql - 如何选择过去 6 个月内存在的所有记录?
- elasticsearch - 弹性搜索扫描操作不起作用
- python - 运行 Dijkstra 算法后如何获取“for”循环结果并将其保存为列表?
- android - Chrome - Android - 刷新 PWA
- c++ - 如何找到pytorch的torch.bmm的c++源代码