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">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell Height="300">
<ViewCell.ContextActions>
<MenuItem IsDestructive="True" Text="Download" x:Name="DownloadItem" Clicked="DownloadItem_Clicked"></MenuItem>
<Label Text="{Binding UserName}" VerticalOptions="Center" HorizontalOptions="Center"/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
//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 :)
Hello,
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.