我有一个Listview页面,其Itemsource是Class A的可观察集合。现在在listview单元格中我需要分配A类对象的命令。但是命令没有被触发。下面是我的代码。
主页
<ListView x:Name="lstInventoryRequests"
HasUnevenRows="True"
ItemsSource="{Binding LstInventoryRequests}" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell >
<ViewCell.View>
<StackLayout Orientation="Horizontal"
HorizontalOptions="FillAndExpand"
VerticalOptions="Fill">
<Label Text="{Binding ItemName}"/>
<Button Text="Acknowledge"
Command="{Binding AcknowledgeInventoryItemsReceive}"
CommandParameter="{Binding .}"/>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
网页浏览模型
LstInventoryRequests = new Observablecollection(){new A(){ItemName= "someitem"}}
ItemSource类
class A
{
public Command<InventoryRequestsDetails> AcknowledgeInventoryItemsReceive;
public A()
{
AcknowledgeInventoryItemsReceive = new Command<InventoryRequestsDetails>(async (arg) => await Handler(arg));
}
private async Task Handler(InventoryRequestsDetails arg)
{
//MyLogic
}
}
现在Button命令没有点击“Handler”方法。任何人都可以帮我识别;我的代码出错了。
答案 0 :(得分:0)
尝试设置BindingContext
<Button Text="Acknowledge" Command="{Binding AcknowledgeInventoryItemsReceive}" BindingContext="{Binding Source={x:Reference lstInventoryRequests}, Path=BindingContext}" CommandParameter="{Binding .}"/>
答案 1 :(得分:0)
您要绑定的数据(在viewmodel中)应声明为Property
public Command<InventoryRequestsDetails> AcknowledgeInventoryItemsReceive { set; get; }