I'm converting a C# layout to XAML. The layout displays a list of Contacts
that each have an Id
, a PhoneNumber
, and a Name
property. I have a method CallContact
that accepts a Contact
as a parameter. It fires off when the user taps a phone image that is in each cell (they access a details page by tapping elsewhere in the cell) The method displays a prompt like "Would you like to call <Name
>?", so it needs both the Name
and the PhoneNumber
properties of the Contact
object.
Previously, I had a C# file for my ViewCell
that looked like this
C# ViewCell
public class ContactCell : ViewCell
{
public ContactCell()
{
var lblName = new Label{Font = Font.SystemFontOfSize(NamedSize.Medium)};
lblName.SetBinding(Label.TextProperty, "Name");
var phoneImage = new Image
{
Source = "Images/phone.png"
};
var phoneImageTgr = new TapGestureRecognizer();
phoneImageTgr.Tapped += (s, e) =>
{
var contact = BindingContext as Contact;
if (contact != null)
CallContact(contact);
};
phoneImage.GestureRecognizers.Add(phoneImageTgr);
View = new StackLayout
{
HorizontalOptions = LayoutOptions.FillAndExpand,
Orientation = StackOrientation.Horizontal,
Children = { phoneImage, lblName }
};
}
private void CallContact(Contact contact)
{
}
}
So I referenced the BindingContext
of the cell to get the specific Contact
. Is there anyway for me, from XAML, to reference the current BindingContext
of the cell and pass it as a CommandParameter
?
XAML
<StackLayout>
<ListView ItemsSource="{Binding Contacts}" SelectedItem="{Binding SelectedItem}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Image Source="Images/phone.png">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding CallContactCommand}" />
</Image.GestureRecognizers>
</Image>
<Label Font="Medium" Text="{Binding Name}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>