【发布时间】:2020-08-21 17:31:45
【问题描述】:
ListView --> ItemSource = { Binding Items } //Works
MenuItem --> Command={ Binding MarkAsCompleted } // 不工作
这只有在我添加路径和来源时才有效
Command="{Binding Path=BindingContext.MarkAsCompletedCommand, Source={x:Reference TodoViewPage}}"
为什么会这样?!?
<ListView ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Text="Complete" Command="{Binding Path=BindingContext.MarkAsCompletedCommand, Source={x:Reference TodoViewPage}}" CommandParameter="{Binding .}"/>
</ViewCell.ContextActions>
<StackLayout>
<Label Text="{Binding Name}"></Label>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
【问题讨论】:
-
Items 中每个 Item 的 ViewModel 是否有
MarkAsCompleted方法?或者MarkAsCompletedCommand在哪里? -
MarkasCompleted 在 viewmodel 类中,我有一个单独的 todoItem 类,它将一个 todoitem 对象列表发送到 viewmodel。不是每个 ToDoItem 都有这个功能。 (我的意思是标记完成功能不在 ToDoItem 类中,它是 viewmodel 类中的常见功能)。
标签: c# xamarin xamarin.forms