itemsource对象xamarin的绑定命令

时间:2017-11-24 15:09:39

标签: xamarin xamarin.ios xamarin.forms xamarin.android

我有一个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”方法。任何人都可以帮我识别;我的代码出错了。

2 个答案:

答案 0 :(得分:0)

尝试设置BindingContext

<Button Text="Acknowledge" Command="{Binding AcknowledgeInventoryItemsReceive}" BindingContext="{Binding Source={x:Reference lstInventoryRequests}, Path=BindingContext}" CommandParameter="{Binding .}"/>

答案 1 :(得分:0)

您要绑定的数据(在viewmodel中)应声明为Property

请参阅:Data Binding Basics

修改:

public Command<InventoryRequestsDetails> AcknowledgeInventoryItemsReceive { set; get; }