1

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>

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

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"); 
}

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.
}

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

How can I accomplish this?

Zaxter
  • 2,939
  • 3
  • 31
  • 48
  • 1
    Maybe you can try ((MenuItem)sender).DataContext to determine the item. – Kai Brummund Jan 16 '15 at 20:00
  • 1
    http://stackoverflow.com/questions/4739282/contextmenu-placementtarget-is-not-getting-set-no-idea-why/15336766#15336766 Shows you ways to get your datacontext through to your context menu. – TYY Jan 16 '15 at 20:04
  • Thanks @KaiBrummund and TTY. I was trying to add the dataContext by enclosing the menuItems in a contextMenu, but I get the error: Type ContextMenu not found in xmlns http://xamarin.com/schemas/2014/forms. – Zaxter Jan 16 '15 at 20:17
  • Does that mean that xamarin's xaml doesn't (as of yet) support contextMenu or am I missing something? – Zaxter Jan 16 '15 at 20:26
  • I just read that in xamarin dataContext is termed as bindingContext. Was able to solve my problem as using your comments. Thanks a ton! Kai Brummund and @TYY – Zaxter Jan 16 '15 at 20:37
  • 2
    Employee selectedEmployee = (Employee)mi.BindingContext; I've tried this & it works for me. – Supreet Feb 19 '15 at 12:14

1 Answers1

2

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

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

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"); 
 }
Trevi Awater
  • 2,387
  • 2
  • 31
  • 53
bashahul
  • 422
  • 1
  • 4
  • 17