How To Get ObservableCollection Strings in ContextAction Menu Item Click Event For Listview Xamarin.forms

SimonGhost 256 Reputation points

Hello Everyone i Have A List View With Label Have Binding Data and My List View Have Context Action inside ViewCell and I Want Get current binding for (Name) String When ContextAction Menu Item Click Event.
My Code:

 ObservableCollection<Employee> employees = new ObservableCollection<Employee>();
public ObservableCollection<Employee> Employees { get { return employees; } }

//binding string class
public class Employee
public string Name { get; set; }

 <ListView x:Name="ListView" SelectionMode="None" RowHeight="200">
                        <ViewCell Height="300">
                                <MenuItem IsDestructive="True" Text="Download"  x:Name="DownloadItem" Clicked="DownloadItem_Clicked"></MenuItem>
                                <Label Text="{Binding UserName}" VerticalOptions="Center" HorizontalOptions="Center"/>

//and this menu item click

 private void DownloadItem_Clicked(object sender, EventArgs e)
            //here i want to get The binding data for current item

//also i test this but not working only in listview item click
var Data = e.Item as Employee;
//and get it like this
DisplayAlert("Your Name is ",Data.Name,"Ok");

Thanks :)

A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,289 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,936 Reputation points


    Welcome to our Microsoft Q&A platform!

    You could get the MenuItem via the sender, then get the BindingContext of the menuItem to retrive the data.

    Check the code:

       void OnItemClicked(object sender, EventArgs e)  
           // The sender is the menuItem  
           MenuItem menuItem = sender as MenuItem;  
           var contextItem = menuItem.BindingContext as Employee;  
          DisplayAlert("Your Name is ",contextItem.Name,"Ok");  

    Best Regards,

    Jarvan Zhang

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

0 additional answers

Sort by: Most helpful