I got a ViewModel
with a command (AddToFavoriteCommand
) that doesn't get called. Now it only looks after the command in the CustomPin class
, not the viewModel
. I'm setting my viewModel
to the BindingContext
of the page in the code behind.
But since it enumerates my collection of customPins, it looks after the command there. I need to get back to the root. I probably need to change the source but can't get it to work.
<ContentPage.Content>
<StackLayout>
<AbsoluteLayout>
<Button Text="{Binding Filter}" Command="{Binding GotoFilterPageCommand}" />
</AbsoluteLayout>
<ListView x:Name="ListView" RowHeight="60" ItemsSource="{Binding CustomPins}" ItemSelected="OnItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Text="Favorite" Command="{Binding AddToFavoriteCommand}" />
<MenuItem Text="..." CommandParameter="{Binding .}" Clicked="OnMoreClicked" />
</ViewCell.ContextActions>
<StackLayout Padding="5" Orientation="Vertical" >
<Label Text="{Binding ParkingLot.Street}" FontSize="Medium" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
Code behind (removed all other logic like clicked events that I didn't needed for this)
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ParkingListPage : ContentPage
{
public ParkingListPage()
{
InitializeComponent();
BindingContext = new ParkingListViewModel();
}
}
My ViewModel
public class ParkingListViewModel
{
public ParkingListViewModel()
{
AddToFavoriteCommand = new Command(Test);
}
private void Test()
{
}
public IEnumerable<CustomPin> CustomPins { get; set; } = SampleParkings.Parkings;
public Command AddToFavoriteCommand { get; }
}
Try it like this:
<ContentPage x:Name="YourPageName">
<ContentPage.Content>
<StackLayout>
<AbsoluteLayout>
<Button Text="{Binding Filter}" Command="{Binding GotoFilterPageCommand}" />
</AbsoluteLayout>
<ListView x:Name="ListView" RowHeight="60" ItemsSource="{Binding CustomPins}" ItemSelected="OnItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Text="Favorite" Command="{Binding Path=BindingContext.AddToFavoriteCommand, Source={x:Reference YourPageName}}" />
<MenuItem Text="..." CommandParameter="{Binding .}" Clicked="OnMoreClicked" />
</ViewCell.ContextActions>
<StackLayout Padding="5" Orientation="Vertical" >
<Label Text="{Binding ParkingLot.Street}" FontSize="Medium" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
Notice how I added the x:Name
element to the root of the page. Of course there are more attributes there, leave them there, but I left them out for clarity here.
Secondly, notice how I referred to that name from the MenuItem
binding and added Path=BindingContext.
. This way it will bind to the BindingContext
of the element identified by the name, in our case the ContentPage
.
A sample project can be found here: https://github.com/jfversluis/SampleParentBinding
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With