如何知道单击了contextAction menuItem的listView viewCell? [英] How to know the listView viewCell whose contextAction menuItem is clicked?

查看:56
本文介绍了如何知道单击了contextAction menuItem的listView viewCell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在xaml中创建了一个listView(使用xamarin在线跨平台开发指南),如下所示:

I have a listView created in xaml (using the xamarin online cross-platform dev guide) like so:

<ListView x:Name="AccountsList" ItemSelected="AccountSelected">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
        <ViewCell.ContextActions>
            <MenuItem Clicked="OnEdit"     
               Text="Edit" />
            <MenuItem Clicked="OnDelete"     
               Text="Delete" IsDestructive="True" />
         </ViewCell.ContextActions> 
          <ViewCell.View>
              <StackLayout Orientation="Vertical"
                           HorizontalOptions="StartAndExpand">

                <Label Text="{Binding DeviceName}"
                       HorizontalOptions="CenterAndExpand"/>
              </StackLayout>
          </ViewCell.View>
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>

我正在像这样以编程方式分配ItemSource:AccountsList.ItemsSource = App.Database.GetAccounts();

I'm assigning the ItemSource programmatically like so: AccountsList.ItemsSource = App.Database.GetAccounts ();

这是我的"onDelete"方法:

And here's my 'onDelete' method:

public void OnDelete (object sender, EventArgs e) { 
    var mi = ((MenuItem)sender); 
    DisplayAlert("Delete Context Action", mi.Command + " delete context action", "OK"); 
}

我想知道单击菜单项的特定viewCell,即从上述"onDelete"方法引用该对象的"DeviceName".我知道我可以使用listView的"ItemSelected"方法来做到这一点:

I want to be able to know the specific viewCell whose menuItem was clicked, i.e. to reference the 'DeviceName' of the object from the above 'onDelete' method. I know I can do this with the 'ItemSelected' method of a listView like this:

void AccountSelected(object sender, SelectedItemChangedEventArgs e)
{
    var _account = (Account)e.SelectedItem;
    // And access the account here.
}

但是'Clicked'方法不允许'void OnDelete(对象发送者,SelectedItemChangedEventArgs e)'签名(而且我怀疑它是否仍然可以工作).

But the 'Clicked' method doesn't allow a 'void OnDelete (object sender, SelectedItemChangedEventArgs e)' signature (and I doubt it would work anyways).

我该怎么做?

推荐答案

您可以绑定类似于以下示例的行的记录ID,

You can bind record id of the row similar to the below example,

<MenuItem Clicked="OnDelete" Text="Delete" IsDestructive="True" CommandParameter="{Binding RecordIdentifier}"/>

在您的OnDelete事件处理程序上,您可以从CommandParameter中获取我们之前绑定的记录ID,类似于下面的示例

And on your OnDelete event handler, you can get the record id we bind earlier from the CommandParameter similar to the below example,

public void OnDelete (object sender, EventArgs e) 
 { 
      var mi = ((MenuItem)sender); 
      DisplayAlert("Delete Context Action", mi.CommandParameter + " delete context action", "OK"); 
 }

这篇关于如何知道单击了contextAction menuItem的listView viewCell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆