Reputation: 55
I have the following XAML code:
<ContentPage x:DataType="viewmodel:MonkeyViewModel">
<CollectionView ItemsSource="{Binding MonkeyList}">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="model:Monkey">
<Label Text="{Binding Id}" />
<Label Text="{Binding Name}" />
<Label Text="Edit">
<Label.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding EditCommand}"
CommandParameter="{Binding Id}"/>
</Label.GestureRecognizers>
</Label>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ContentPage>
The MonkeyList
and EditCommand
come from viewmodel:MonkeyViewModel
.
The Id
and Name
come from model:Monkey
.
On the tag <TapGestureRecognizer>
, the {Binding EditCommand}
doesn't work due to the EditCommand
not being found in the data context of Monkey
.
How can I make the EditCommand
binding on MonkeyViewModel
and Id
binding on Monkey
?
I would expect the <Label>
works like a html <A>
link: <a href="javascript:edit(id)">Edit</a>
or: <a onclick="edit(id)">Edit</a>
Upvotes: 0
Views: 945
Reputation: 8914
Just change the Command binding to use a Relative Binding as follows:
<TapGestureRecognizer
Command="{Binding EditCommand, Source={RelativeSource AncestorType={x:Type viewmodel:MonkeyViewModel}}}"
CommandParameter="{Binding Id}"/>
Upvotes: 2